Archive for the ‘Ajax’ Category
Saturday, January 19th, 2008
Download Source | View Demo
One of the advanced features of Struts2 is its plugin support. By using a standard interface API, you can seamlessly extend Struts2 capability.
Plugins are packaged as jar files along with any dependency jars. To use a plugin, you need to add it to the classpath. Struts automatically configures a plugin by using struts-plugin.xml packaged as part of the plugin jar.
When you use external plugins (plugins which are not part of standard Struts2 distribution) you must be aware of the risks. The plugin may not be fully tested and there could be other hidden issues. So always evaluate an external plugin before using it in a production server.
In this post, I will look at a very useful external Struts2 plugin - Monitored Ajax File Upload. Here is are the steps required for a sample demo.

1. Download Ajax File Upload plugin - You can download this from here.
2. Extract the zip and then copy the jars to WEB-INF/lib of your Web application. The jars are json-lib-2.0, commons-io-1.3.1, commons-fileupload-1.2 and AjaxFileUpload-0.03
3. Now create a FileUploadDemoAction action class extending from com.davidjc.ajaxfileupload.action.FileUpload. This is given below.
4. Configure struts.xml with the action and along with an additional interceptor as given below.
5. Create a JSP which contains file upload control. You need to use the ajaxfileuploadform custom tag. In this example, this jsp is ajaxupload.jsp.
6. This added files are displayed below the upload control using Ajax. For this YUI component connect is used for the GET request. The request is made to FileUploadDetailsAction and the results are returned using uploadlist.jsp. This content is added to the div tag (with id “did”) in the main page. Note how the ajax call is done from show_list method. This method is given as doafter parameter which is a JavaScript function executed immediately after file upload.
The above code also illustrates usage OGNL expressions and the use of status attribute on iterator tag for handling even/odd rows.
7. Configure the server location of the uploaded temporary file by setting struts.multipart.saveDir property. This can be done in struts.properties or web.xml. In this sample, it is set to c:/temp (windows machine) in web.xml. Make sure that the directory exists on the filesystem. In this example I have also set the maximum upload file size to 1MB using struts.multipart.maxSize property.
Complete details of the uploaded file is available in the FileUploadDemoAction. The temporary file created during upload is deleted after sometime. So you can use the File object to save the content to a separate folder or to a database.
To test this sample, use the JSP URL directly : http://localhost:8080/struts2demo/ajaxupload.jsp
Important: Ensure that you have Log4J jar in your application server’s lib folder.Also I am aware that this example gives errors and is not properly showing progress bar. I am still investigating these problems.
Got any questions? Don’t hesitate to contact me. Greetings to Dave Casserly for this excellent plugin.
Posted in Ajax, Howto, Struts2 Plugins, YUI Integration | 5 Comments »
Saturday, January 19th, 2008
Download Source | View Demo
Yahoo User Interface (YUI) library is a set of Javascript based client side components for building highly interactive Web applications. All the YUI components work on the client side data. It also has built in Ajax support.
By integrating YUI to your Struts2 application, you can create cutting edge Web 2.0 rich client applications. In this post, I will show you how the YUI Menu component can be used along with Struts2 to create a cascading application menu.

Following are the components used in this sample project.
1. struts.xml - Struts2 action mappings.
2. web.xml - Configuration file for the sample web application
3. Menu.java - This encapsulates a Menu object. Can contain sub menus.
4. MenuDemoAction.java - Controller class for Menu demo. Contains code to populate sample Menu data. In real projects, you will populate this from database.
5. menudemo.jsp - This is a sample application screen with application menu at the top.
6. AppMenuTag.java - This is a custom tag library which renders a YUI based Menu bar.
7. menu.tld - The tag library configuration file inside WEB-INF folder.
In addition, this example uses the following css and JavaScript files provided as part of YUI. In real projects, these also must be part of your project.
8. http://yui.yahooapis.com/2.4.1/build/menu/assets/skins/sam/menu.css
9. http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js
10. http://yui.yahooapis.com/2.4.1/build/container/container_core-min.js
11. http://yui.yahooapis.com/2.4.1/build/menu/menu-min.js
12. http://yui.yahooapis.com/2.4.1/build/json/json-beta-min.js
Now let us see how the code works.
When user accesses http://localhost:8080/struts2demo/MenuDemoAction.action, MenuDemoAction’s execute is invoked. This prepares the Menu data. A list of Menu objects is created. Each of this in turn can contain sub menus. After this control is passed to menudemo.jsp. In real applications, this will be fed from an XML file or from database tables.
menudemo.jsp contains AppMenuTag and it is invoked during JSP rendering. This converts the Menu object structure to a JSON structure using a YUI library. This JSON structure is given as input to the YUI menu control which renders the CSS based menu on the screen! Can it be any simpler? This whole project took me about 20 minutes to implement and test.
Now using similar techniques you can implement a wide range of controls available in YUI. These include tree, grid etc. which are frequently required in Web applications. YUI also contains utility functions such as a JSON library and methods for AJax interaction (YUI Connect).
Posted in Ajax, Howto, Struts2 Tips, YUI Integration | No Comments »
Wednesday, January 16th, 2008
Download Source | View Demo
Important : This example uses Struts 2.0.11. Struts2 Ajax support is experimental is undergoing rapid changes. I will update this example once Struts 2.1 is released.
One of the biggest improvements in Struts2 compared to Struts is its first class AJAX support. All the core Struts2 components are developed with a view of deploying in a highly responsive AJAX mode. In this post, I will look at the Struts2 div tag which can be used to render Ajax content. This component internally uses Dojo library for Ajax functionality.
To demonstrate this, I will create a very simple use case. The use case consists of a single screen which lists a number of users in a table. When a userid link is clicked, user details is shown at the bottom. For displaying user details we will use Ajax.
Following are the files used in this sample Struts2 project. The code listing is also given below.
1. web.xml
J2EE configuration file
2. struts.xml
Mvc configuration for struts
3. userlisting.jsp
4. userdetail.jsp
This displays user details and is loaded using Ajax from userlisting.jsp
5. UserListingAction.java (under package ajaxdemo/action)
Prepares data for user listing and dispatches to userlisting.jsp. In a real application, this will connect to datasource through a business layer.
6. UserDetailAction.java (under package ajaxdemo/action)
This loads the data when a userid is selected in userlisting.jsp. This is called via Dojo Ajax.
7. UserListDTO.java (under package ajaxdemo/dto
This encapsulates details of a single user displayed in the list. Contains id and name. DTO stands for Data Transfer Object.
8. UserDetailDTO.java (unser package ajaxdemo/dto)
This encapsulates the complete details of a specific user.
When you access the URL http://localhost:8084/ajaxdemo/UserListingAction.action, UserListingAction prepares the data and it forwards to the userlisting.jsp. For Ajax, we use div tag in Struts2 tag library. Whenever a userid is clicked in the list, JavaScript function notifies the div tag to dynamically load content from a URL. In this case it is UserDetailAction which in turn gets data corresponding to the selected userid and dispatches to userdetail.jsp. The content returned by userdetail.jsp is loaded under Struts div tag.
As you can see the entire Ajax plumbing is hidden from the application developer and it is possible to quickly build highly responsive Web applications using this simple technique.
This sample application is hosted here. Check out to see the beauty of Struts2 Ajax. You can also download the entire source as a war file.
Note: We don’t want the user details to be loaded initially. But as of Struts 2.0.11, it is not possible to prevent the initial Ajax call. It seems that Struts 2.1 has introduced an attribute “preload” for preventing initial Ajax load. The work around here is to check the userid before returning any content.
In the next part we will look at each of the files in detail.
Posted in Ajax, Struts 2.0.11, Tutorials | 3 Comments »