- 浏览: 258175 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
jeans_1312:
...
JAVA检测字符串编码并转换 -
zenzuguo:
为啥我在eclipse tomcat6.0启动时总报错
Ma ...
appfuse2.0.2在eclipse下的开发和使用 -
josewu2009:
楼主我用你的方法在window机器 上 压缩后 ,解压文件时报 ...
java压缩文件夹下的所有文件和选定文件及乱码解决 -
TonyLian:
我觉得2楼提出的,只不过是看起来更漂亮而已,原写法也不会出现B ...
Ibatis动态查询例子(#和$以及iterate等的用法) -
zhaoshg:
谢谢 lemonweirui 的更正,帮我发现了一个bug,谢 ...
Ibatis动态查询例子(#和$以及iterate等的用法)
转自http://www.struts2.org/struts2-and-ajax-using-dojo-div-part-i
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
Displays list of users
Displays list of users
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.
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 version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2. struts.xml
Mvc configuration for struts
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="ajaxdemo" extends="struts-default"> <action name="UserListingAction" class="ajaxdemo.action.UserListingAction"> <result>/userlisting.jsp</result> </action> <action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction"> <result>/userdetail.jsp</result> </action> </package> </struts>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="ajaxdemo" extends="struts-default"> <action name="UserListingAction" class="ajaxdemo.action.UserListingAction"> <result>/userlisting.jsp</result> </action> <action name="UserDetailAction" class="ajaxdemo.action.UserDetailAction"> <result>/userdetail.jsp</result> </action> </package> </struts>
3. userlisting.jsp
Displays list of users
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <s:head theme="ajax"/> </head> <script> function show_user_details(id) { document.frm_user.userid.value = id; dojo.event.topic.publish("show_detail"); } </script> <body> <s:form id="frm_user" name="frm_user"> <h1>User Listing</h1> <s:if test="userList.size > 0"> <table border="1"> <s:iterator value="userList"> <tr> <td> <s:a href="#" onclick="javascript:show_user_details('%{id}');return false;"><s:property value="id" /></s:a> </td> <td> <s:property value="name" /> </td> </tr> </s:iterator> </table> </s:if> <s:hidden name="userid"/> <s:url id="d_url" action="UserDetailAction" /> <s:div id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" > </s:div> </s:form> </body> </html>
Displays list of users
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <s:head theme="ajax"/> </head> <script> function show_user_details(id) { document.frm_user.userid.value = id; dojo.event.topic.publish("show_detail"); } </script> <body> <s:form id="frm_user" name="frm_user"> <h1>User Listing</h1> <s:if test="userList.size > 0"> <table border="1"> <s:iterator value="userList"> <tr> <td> <s:a href="#" onclick="javascript:show_user_details('%{id}');return false;"><s:property value="id" /></s:a> </td> <td> <s:property value="name" /> </td> </tr> </s:iterator> </table> </s:if> <s:hidden name="userid"/> <s:url id="d_url" action="UserDetailAction" /> <s:div id="user_details" href="%{d_url}" theme="ajax" listenTopics="show_detail" formId="frm_user" > </s:div> </s:form> </body> </html>
4. userdetail.jsp
This displays user details and is loaded using Ajax from userlisting.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <h1>User Details</h1> <s:if test="userDetails != null"> <table> <tr><td>Id:</td><td><s:property value="userDetails.id" /></td></tr> <tr><td>Name:</td><td><s:property value="userDetails.name" /></td></tr> <tr><td>Email:</td><td><s:property value="userDetails.email" /></td></tr> <tr><td>Address:</td><td><s:property value="userDetails.address" /></td></tr> </table> </s:if>
<%@ taglib prefix="s" uri="/struts-tags" %> <h1>User Details</h1> <s:if test="userDetails != null"> <table> <tr><td>Id:</td><td><s:property value="userDetails.id" /></td></tr> <tr><td>Name:</td><td><s:property value="userDetails.name" /></td></tr> <tr><td>Email:</td><td><s:property value="userDetails.email" /></td></tr> <tr><td>Address:</td><td><s:property value="userDetails.address" /></td></tr> </table> </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.
package ajaxdemo.action; import ajaxdemo.dto.UserListDTO; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.List; /** Populates the user listing data */ public class UserListingAction extends ActionSupport { private List<UserListDTO> userList; // this is available in view automatically! public String execute() throws Exception { // create 2 user objects and add to a list setUserList((List<UserListDTO>) new ArrayList()); UserListDTO user = new UserListDTO(); user.setId("gjose"); user.setName("Grace Joseph"); getUserList().add(user); user = new UserListDTO(); user.setId("peter"); user.setName("PeterSmith"); getUserList().add(user); return SUCCESS; } public List<UserListDTO> getUserList() { return userList; } public void setUserList(List<UserListDTO> userList) { this.userList = userList; } }
package ajaxdemo.action; import ajaxdemo.dto.UserListDTO; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.List; /** Populates the user listing data */ public class UserListingAction extends ActionSupport { private List<UserListDTO> userList; // this is available in view automatically! public String execute() throws Exception { // create 2 user objects and add to a list setUserList((List<UserListDTO>) new ArrayList()); UserListDTO user = new UserListDTO(); user.setId("gjose"); user.setName("Grace Joseph"); getUserList().add(user); user = new UserListDTO(); user.setId("peter"); user.setName("PeterSmith"); getUserList().add(user); return SUCCESS; } public List<UserListDTO> getUserList() { return userList; } public void setUserList(List<UserListDTO> userList) { this.userList = userList; } }
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.
package ajaxdemo.action; import ajaxdemo.dto.UserDetailDTO; import com.opensymphony.xwork2.ActionSupport; /* Populates user details for a user id selected */ public class UserDetailAction extends ActionSupport { private String userid; private UserDetailDTO userDetails; public String execute() throws Exception { // populate only when userid is selected if(userid!=null && !userid.equals("")) populateDetail(userid); return SUCCESS; } private void populateDetail(String id) { userDetails = new UserDetailDTO(); userDetails.setId(id); userDetails.setName("The Complete Name"); userDetails.setEmail("admin@struts2.org"); userDetails.setAddress("rich street, lavish road, Struts Land"); } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public UserDetailDTO getUserDetails() { return userDetails; } public void setUserDetails(UserDetailDTO userDetails) { this.userDetails = userDetails; } }
package ajaxdemo.action; import ajaxdemo.dto.UserDetailDTO; import com.opensymphony.xwork2.ActionSupport; /* Populates user details for a user id selected */ public class UserDetailAction extends ActionSupport { private String userid; private UserDetailDTO userDetails; public String execute() throws Exception { // populate only when userid is selected if(userid!=null && !userid.equals("")) populateDetail(userid); return SUCCESS; } private void populateDetail(String id) { userDetails = new UserDetailDTO(); userDetails.setId(id); userDetails.setName("The Complete Name"); userDetails.setEmail("admin@struts2.org"); userDetails.setAddress("rich street, lavish road, Struts Land"); } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public UserDetailDTO getUserDetails() { return userDetails; } public void setUserDetails(UserDetailDTO userDetails) { this.userDetails = userDetails; } }
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.
package ajaxdemo.dto; public class UserListDTO { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package ajaxdemo.dto; public class UserListDTO { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
8. UserDetailDTO.java (unser package ajaxdemo/dto)
This encapsulates the complete details of a specific user.
package ajaxdemo.dto; public class UserDetailDTO { private String id; private String name; private String email; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
package ajaxdemo.dto; public class UserDetailDTO { private String id; private String name; private String email; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
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.
发表评论
-
struts2的内置拦截器
2012-03-13 20:15 1998<interceptor name=&quo ... -
Struts2的国际化支持
2010-07-29 17:43 1000每种框价都会有国际化的支持,struts2的国际化大致上分为页 ... -
在Struts 2中使用JSON Ajax支持
2009-04-24 13:36 1459JSON插件提供了一种名为json的ResultType,一旦 ... -
Struts2的Result类型
2009-03-30 18:45 2479一个提交到服务器的处理通常可以分为两个阶段,第一个阶段查询服务 ... -
struts2快速上手(附例子)(例子已修改)
2009-03-29 02:02 2594实在对不起,上次写的例子有些匆忙,有些小的细节没有注意到,现在 ... -
struts2 使用validate方法验证数据
2009-03-28 02:07 1296在Struts2中最简单的验证 ... -
struts2 使用Validation框架验证数据
2009-03-28 01:46 1920在《Struts2教程4:使用validate方法验证数据》中 ... -
教你如何使用Struts2拦截器并且定义自己的拦截器
2009-03-28 01:21 16318看到网上很多介绍关于拦截器的文章,感觉都不错,但是都没有很详细 ... -
Struts2, Spring MVC, Sitemesh, Tiles 性能比较
2009-03-27 16:45 7477(本文出自 http://blog.csdn.net/Alex ... -
Struts 2与AJAX
2009-03-27 16:27 1559在当今——Web 2.0概念铺天盖地的Internet环境下, ...
相关推荐
Struts2-dojo-plugin-2.2.1.jar 是一个针对Apache Struts2框架的扩展插件,主要用于增强Struts2应用的用户界面交互性,特别是通过集成Dojo JavaScript库来提供丰富的AJAX功能和用户体验。这个插件是Struts2与Dojo ...
struts2-dojo-plugin-2.3.4 jar 日期控件的jar包 需要的自行下载
struts2-dojo 跨域时解决拒绝访问的问题
Struts2的使用中出现的版本不符合或者缺少struts2-dojo-plugin-2.3.16的问题,大家可以下载使用。
struts2-dojo-plugin-2.3.8
struts2-dojo-plugin-2.3.1.2.jar
struts2-dojo-plugin-2.1.0的jar包,可以再struts中使用dojo......
在"struts2-dojo-plugin-2.3.15.3.jar"这个文件中,包含了实现Struts2与Dojo集成所需的所有类和资源。这包括Action、Interceptor、Result以及其他辅助类,它们使得Struts2能够识别并处理Dojo相关的请求。此外,还...
struts2-dojo-plugin-2.3.24日期控件9-12月乱码解决后jar包
struts2-dojo-plugin-2.1.6.jar 解决了datetimepicker显示月份乱码的问题,显示9、10、11、12月份时出现乱码,本包修改了该bug
struts2-struts1-plugin-2.1.6.jar
struts2-dojo-plugin-2.1.2.jar CSDN大多下载不了,所以上传!
为了在Struts2中集成AJAX功能,开发者通常需要借助特定的插件,比如我们这里提到的"struts2-dojo-plugin-2.2.3.1"。 "struts2-dojo-plugin-2.2.3.1.zip_key_可用struts的ajax支持插件"这个标题表明,这个压缩包提供...
struts2-dojo-plugin-2.2.3.jar struts2-dojo-plugin-2.2.3.jar
修正struts2的datetimepicker控件中九月后月份显示为乱码现象
修正了struts2.3.15中使用标签月份中文乱码的问题,直接替换即可
这是最新的struts2日历控件的jar文件
解决struts2 日期选择器控件乱码问题后的jar包。
包中包含strut2示例,struts2所需jar包,以及配置文件,struts2包中还包含了开发文档