分享

Struts2 and AJAX - Using Dojo Div

 LibraryPKU 2013-11-19
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


Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.5" xmlns="http://java./xml/ns/javaee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd">    
  3.   <filter>    
  4.     <filter-name>struts2</filter-name>    
  5.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    
  6.   </filter>    
  7.   <filter-mapping>    
  8.     <filter-name>struts2</filter-name>    
  9.     <url-pattern>/*</url-pattern>    
  10.   </filter-mapping>    
  11. </web-app>    
  12.   
  13. <?xml version="1.0" encoding="UTF-8"?>    
  14. <web-app version="2.5" xmlns="http://java./xml/ns/javaee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd">    
  15.   <filter>    
  16.     <filter-name>struts2</filter-name>    
  17.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    
  18.   </filter>    
  19.   <filter-mapping>    
  20.     <filter-name>struts2</filter-name>    
  21.     <url-pattern>/*</url-pattern>    
  22.   </filter-mapping>    
  23. </web-app>    

2. struts.xml
Mvc configuration for struts


Xml代码  收藏代码
  1. <!DOCTYPE struts PUBLIC    
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  3.     "http://struts./dtds/struts-2.0.dtd">    
  4. <struts>    
  5.     <package name="ajaxdemo" extends="struts-default">    
  6.         <action name="UserListingAction" class="ajaxdemo.action.UserListingAction">    
  7.             <result>/userlisting.jsp</result>    
  8.         </action>    
  9.         <action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction">    
  10.             <result>/userdetail.jsp</result>    
  11.         </action>    
  12.     </package>     
  13. </struts>    


Xml代码  收藏代码
  1. <!DOCTYPE struts PUBLIC    
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  3.     "http://struts./dtds/struts-2.0.dtd">    
  4. <struts>    
  5.     <package name="ajaxdemo" extends="struts-default">    
  6.         <action name="UserListingAction" class="ajaxdemo.action.UserListingAction">    
  7.             <result>/userlisting.jsp</result>    
  8.         </action>    
  9.         <action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction">    
  10.             <result>/userdetail.jsp</result>    
  11.         </action>    
  12.     </package>     
  13. </struts>    

3. userlisting.jsp


Displays list of users 
Jsp代码  收藏代码
  1. <%@ taglib prefix="s" uri="/struts-tags" %>    
  2. <html>    
  3.   <head>    
  4.     <s:head theme="ajax"/>    
  5.         
  6.   </head>    
  7.   <script>    
  8.     function show_user_details(id) {    
  9.       document.frm_user.userid.value = id;    
  10.       dojo.event.topic.publish("show_detail");    
  11.     }    
  12.   </script>    
  13.   <body>    
  14.     <s:form id="frm_user" name="frm_user">    
  15.       <h1>User Listing</h1>    
  16.       <s:if test="userList.size > 0">    
  17.         <table border="1">    
  18.           <s:iterator value="userList">    
  19.             <tr>    
  20.               <td>    
  21.                 <s:a href="#" onclick="javascript:show_user_details('%{id}');return false;"><s:property value="id" /></s:a>    
  22.               </td>    
  23.               <td>    
  24.                 <s:property value="name" />    
  25.               </td>    
  26.             </tr>    
  27.           </s:iterator>    
  28.         </table>    
  29.       </s:if>     
  30.       <s:hidden name="userid"/>    
  31.       <s:url id="d_url" action="UserDetailAction" />    
  32.       <s:div  id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" >    
  33.       </s:div>    
  34.     </s:form>    
  35.   </body>    
  36. </html>    



Displays list of users 
Jsp代码  收藏代码
  1. <%@ taglib prefix="s" uri="/struts-tags" %>    
  2. <html>    
  3.   <head>    
  4.     <s:head theme="ajax"/>    
  5.         
  6.   </head>    
  7.   <script>    
  8.     function show_user_details(id) {    
  9.       document.frm_user.userid.value = id;    
  10.       dojo.event.topic.publish("show_detail");    
  11.     }    
  12.   </script>    
  13.   <body>    
  14.     <s:form id="frm_user" name="frm_user">    
  15.       <h1>User Listing</h1>    
  16.       <s:if test="userList.size > 0">    
  17.         <table border="1">    
  18.           <s:iterator value="userList">    
  19.             <tr>    
  20.               <td>    
  21.                 <s:a href="#" onclick="javascript:show_user_details('%{id}');return false;"><s:property value="id" /></s:a>    
  22.               </td>    
  23.               <td>    
  24.                 <s:property value="name" />    
  25.               </td>    
  26.             </tr>    
  27.           </s:iterator>    
  28.         </table>    
  29.       </s:if>     
  30.       <s:hidden name="userid"/>    
  31.       <s:url id="d_url" action="UserDetailAction" />    
  32.       <s:div  id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" >    
  33.       </s:div>    
  34.     </s:form>    
  35.   </body>    
  36. </html>    

4. userdetail.jsp
This displays user details and is loaded using Ajax from userlisting.jsp


Jsp代码  收藏代码
  1. <%@ taglib prefix="s" uri="/struts-tags" %>    
  2. <h1>User Details</h1>    
  3. <s:if test="userDetails != null">    
  4.     <table>    
  5.       <tr><td>Id:</td><td><s:property value="userDetails.id" /></td></tr>    
  6.       <tr><td>Name:</td><td><s:property value="userDetails.name" /></td></tr>    
  7.       <tr><td>Email:</td><td><s:property value="userDetails.email" /></td></tr>    
  8.       <tr><td>Address:</td><td><s:property value="userDetails.address" /></td></tr>    
  9.     </table>    
  10. </s:if>    



Jsp代码  收藏代码
  1. <%@ taglib prefix="s" uri="/struts-tags" %>    
  2. <h1>User Details</h1>    
  3. <s:if test="userDetails != null">    
  4.     <table>    
  5.       <tr><td>Id:</td><td><s:property value="userDetails.id" /></td></tr>    
  6.       <tr><td>Name:</td><td><s:property value="userDetails.name" /></td></tr>    
  7.       <tr><td>Email:</td><td><s:property value="userDetails.email" /></td></tr>    
  8.       <tr><td>Address:</td><td><s:property value="userDetails.address" /></td></tr>    
  9.     </table>    
  10. </s:if>    

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.



Java代码  收藏代码
  1. package ajaxdemo.action;    
  2.     
  3. import ajaxdemo.dto.UserListDTO;    
  4. import com.opensymphony.xwork2.ActionSupport;    
  5. import java.util.ArrayList;    
  6. import java.util.List;    
  7.     
  8. /** Populates the user listing data */    
  9. public class UserListingAction extends ActionSupport {    
  10.     
  11.     private List<UserListDTO> userList; // this is available in view automatically!    
  12.     public String execute() throws Exception {    
  13.             
  14.         // create 2 user objects and add to a list    
  15.         setUserList((List<UserListDTO>) new ArrayList());    
  16.         UserListDTO user = new UserListDTO();    
  17.         user.setId("gjose");    
  18.         user.setName("Grace Joseph");    
  19.         getUserList().add(user);    
  20.             
  21.         user = new UserListDTO();    
  22.         user.setId("peter");    
  23.         user.setName("PeterSmith");    
  24.         getUserList().add(user);    
  25.         return SUCCESS;    
  26.     }    
  27.     
  28.     public List<UserListDTO> getUserList() {    
  29.         return userList;    
  30.     }    
  31.     
  32.     public void setUserList(List<UserListDTO> userList) {    
  33.         this.userList = userList;    
  34.     }    
  35. }    



Java代码  收藏代码
  1. package ajaxdemo.action;    
  2.     
  3. import ajaxdemo.dto.UserListDTO;    
  4. import com.opensymphony.xwork2.ActionSupport;    
  5. import java.util.ArrayList;    
  6. import java.util.List;    
  7.     
  8. /** Populates the user listing data */    
  9. public class UserListingAction extends ActionSupport {    
  10.     
  11.     private List<UserListDTO> userList; // this is available in view automatically!    
  12.     public String execute() throws Exception {    
  13.             
  14.         // create 2 user objects and add to a list    
  15.         setUserList((List<UserListDTO>) new ArrayList());    
  16.         UserListDTO user = new UserListDTO();    
  17.         user.setId("gjose");    
  18.         user.setName("Grace Joseph");    
  19.         getUserList().add(user);    
  20.             
  21.         user = new UserListDTO();    
  22.         user.setId("peter");    
  23.         user.setName("PeterSmith");    
  24.         getUserList().add(user);    
  25.         return SUCCESS;    
  26.     }    
  27.     
  28.     public List<UserListDTO> getUserList() {    
  29.         return userList;    
  30.     }    
  31.     
  32.     public void setUserList(List<UserListDTO> userList) {    
  33.         this.userList = userList;    
  34.     }    
  35. }    

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.


Java代码  收藏代码
  1. package ajaxdemo.action;    
  2.     
  3. import ajaxdemo.dto.UserDetailDTO;    
  4. import com.opensymphony.xwork2.ActionSupport;    
  5.     
  6. /* Populates user details for a user id selected */    
  7. public class UserDetailAction extends ActionSupport {    
  8.         
  9.     private String userid;    
  10.     private UserDetailDTO userDetails;    
  11.         
  12.     public String execute() throws Exception {    
  13.         // populate only when userid is selected    
  14.         if(userid!=null && !userid.equals(""))    
  15.             populateDetail(userid);    
  16.         return SUCCESS;    
  17.     }    
  18.         
  19.     private void populateDetail(String id) {    
  20.         userDetails = new UserDetailDTO();    
  21.         userDetails.setId(id);    
  22.         userDetails.setName("The Complete Name");    
  23.         userDetails.setEmail("admin@struts2.org");    
  24.         userDetails.setAddress("rich street, lavish road, Struts Land");    
  25.     }    
  26.     
  27.     public String getUserid() {    
  28.         return userid;    
  29.     }    
  30.     
  31.     public void setUserid(String userid) {    
  32.         this.userid = userid;    
  33.     }    
  34.     
  35.     public UserDetailDTO getUserDetails() {    
  36.         return userDetails;    
  37.     }    
  38.     
  39.     public void setUserDetails(UserDetailDTO userDetails) {    
  40.         this.userDetails = userDetails;    
  41.     }    
  42.     
  43. }    



Java代码  收藏代码
  1. package ajaxdemo.action;    
  2.     
  3. import ajaxdemo.dto.UserDetailDTO;    
  4. import com.opensymphony.xwork2.ActionSupport;    
  5.     
  6. /* Populates user details for a user id selected */    
  7. public class UserDetailAction extends ActionSupport {    
  8.         
  9.     private String userid;    
  10.     private UserDetailDTO userDetails;    
  11.         
  12.     public String execute() throws Exception {    
  13.         // populate only when userid is selected    
  14.         if(userid!=null && !userid.equals(""))    
  15.             populateDetail(userid);    
  16.         return SUCCESS;    
  17.     }    
  18.         
  19.     private void populateDetail(String id) {    
  20.         userDetails = new UserDetailDTO();    
  21.         userDetails.setId(id);    
  22.         userDetails.setName("The Complete Name");    
  23.         userDetails.setEmail("admin@struts2.org");    
  24.         userDetails.setAddress("rich street, lavish road, Struts Land");    
  25.     }    
  26.     
  27.     public String getUserid() {    
  28.         return userid;    
  29.     }    
  30.     
  31.     public void setUserid(String userid) {    
  32.         this.userid = userid;    
  33.     }    
  34.     
  35.     public UserDetailDTO getUserDetails() {    
  36.         return userDetails;    
  37.     }    
  38.     
  39.     public void setUserDetails(UserDetailDTO userDetails) {    
  40.         this.userDetails = userDetails;    
  41.     }    
  42.     
  43. }    

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.

Java代码  收藏代码
  1. package ajaxdemo.dto;    
  2. public class UserListDTO {    
  3.     private String id;    
  4.     private String name;    
  5.     
  6.     public String getId() {    
  7.         return id;    
  8.     }    
  9.     
  10.     public void setId(String id) {    
  11.         this.id = id;    
  12.     }    
  13.     
  14.     public String getName() {    
  15.         return name;    
  16.     }    
  17.     
  18.     public void setName(String name) {    
  19.         this.name = name;    
  20.     }    
  21.     
  22. }    


Java代码  收藏代码
  1. package ajaxdemo.dto;    
  2. public class UserListDTO {    
  3.     private String id;    
  4.     private String name;    
  5.     
  6.     public String getId() {    
  7.         return id;    
  8.     }    
  9.     
  10.     public void setId(String id) {    
  11.         this.id = id;    
  12.     }    
  13.     
  14.     public String getName() {    
  15.         return name;    
  16.     }    
  17.     
  18.     public void setName(String name) {    
  19.         this.name = name;    
  20.     }    
  21.     
  22. }    

8. UserDetailDTO.java (unser package ajaxdemo/dto)
This encapsulates the complete details of a specific user.


Java代码  收藏代码
  1. package ajaxdemo.dto;    
  2.     
  3. public class UserDetailDTO {    
  4.     
  5.     private String id;    
  6.     private String name;    
  7.     private String email;    
  8.     private String address;    
  9.     
  10.     public String getId() {    
  11.         return id;    
  12.     }    
  13.     
  14.     public void setId(String id) {    
  15.         this.id = id;    
  16.     }    
  17.     
  18.     public String getName() {    
  19.         return name;    
  20.     }    
  21.     
  22.     public void setName(String name) {    
  23.         this.name = name;    
  24.     }    
  25.     
  26.     public String getEmail() {    
  27.         return email;    
  28.     }    
  29.     
  30.     public void setEmail(String email) {    
  31.         this.email = email;    
  32.     }    
  33.     
  34.     public String getAddress() {    
  35.         return address;    
  36.     }    
  37.     
  38.     public void setAddress(String address) {    
  39.         this.address = address;    
  40.     }    
  41. }    


Java代码  收藏代码
  1. package ajaxdemo.dto;    
  2.     
  3. public class UserDetailDTO {    
  4.     
  5.     private String id;    
  6.     private String name;    
  7.     private String email;    
  8.     private String address;    
  9.     
  10.     public String getId() {    
  11.         return id;    
  12.     }    
  13.     
  14.     public void setId(String id) {    
  15.         this.id = id;    
  16.     }    
  17.     
  18.     public String getName() {    
  19.         return name;    
  20.     }    
  21.     
  22.     public void setName(String name) {    
  23.         this.name = name;    
  24.     }    
  25.     
  26.     public String getEmail() {    
  27.         return email;    
  28.     }    
  29.     
  30.     public void setEmail(String email) {    
  31.         this.email = email;    
  32.     }    
  33.     
  34.     public String getAddress() {    
  35.         return address;    
  36.     }    
  37.     
  38.     public void setAddress(String address) {    
  39.         this.address = address;    
  40.     }    
  41. }    

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.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多