转自:http://blog.csdn.net/hanxuemin12345/article/details/38782213
一,引题
1,Json数据格式简介
JSON是脱离语言的理想的数据交换格式,所以它被频繁的应用在客户端与服务器的通信过程中,这一点是毋庸置疑的。而在客户端与服务器的通信过程 中,JSON数据的传递又被分为服务器向客户端传送JSON数据,和客户端向服务器传送JSON数据,前者的核心过程中将对象转换成JSON,而后者的核 心是将JSON转换成对象,这是本质的区别。另外,值得一提的是,JSON数据在传递过程中,其实就是传递一个普通的符合JSON语法格式的字符串而已, 所谓的“JSON对象”是指对这个JSON字符串解析和包装后的结果
2,Struts2返回JSON数据到客户端
这是最常见的需求,在AJAX大行其道的今天,向服务器请求JSON数据已成为每一个WEB应用必备的功能。抛开Struts2暂且不提,在常规WEB应用 中由服务器返回JSON数据到客户端有两种方式:一是在Servlet中输出JSON串,二是在JSP页面中输出JSON串。上文提到,服务器像客户端返 回JSON数据,其实就是返回一个符合JSON语法规范的字符串,所以在上述两种方法中存在一个共同点,就是将需要返回的数据包装称符合JSON语法规范 的字符串后在页面中显示。
3,Struts2 Action使用传统方式返回json数据
省略。。。。。
4,Struts2 集成Json插件,配置json格式结果集,返回json数据
JSON插件是Structs 2 的Ajax插件,通过利用JSON插件,开发者可以很方便,灵活的利用Ajax进行开发。 Json是一种轻量级的数据交换格式,JSon插件提供了一种名为json的Action ResultType 。使用此结果集的好处:将转换JSON数据的工作交给Struts2来做,与Action中以传统方式输出JSON不同的是,Action只需要负责业务处理,而无需关心结果数据是如何被转换成JSON被返回客户端的——这些 工作通过简单的xml配置及jar包引用,Struts2会帮我们做的更好。
二、的实现步骤:
1,引入Struts包、Struts与Json集成的jar包;struts-plugin.xml配置文件
——
——struts-plugin.xml:配置了集成的Json插件的信息(定义了名为"json"的结果集,和名为"json"的拦截器;注:具体json类型的结果集和拦截器Struts的json插件已经实现了,我们只需在配置文件中将了实现类配置上即可);
1 <struts> 2 <package name="json-default"extends="struts-default"> 3 <result-types> 4 <!--名为"json"的结果集--> 5 <result-typenameresult-typename="json" class="org.apache.struts2.json.JSONResult"/> 6 </result-types> 7 <interceptors> 8 <!--名为"json"的拦截器--> 9 <interceptornameinterceptorname="json"class="org.apache.struts2.json.JSONInterceptor"/> 10 </interceptors> 11 </package> 12 </struts>
2,Web.xml
——配置Struts2的核心过滤器
1 <web-appversionweb-appversion="2.5" 2 xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <!-- 配置Struts2的核心的过滤器 --> 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 </filter> 12 <filter-mapping> 13 <filter-name>struts2</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 <welcome-file-list> 17 <welcome-file>index.jsp</welcome-file> 18 </welcome-file-list> 19 </web-app>
3,用户User类——实体类
1 private Long uid;//用户id 2 private String username;//用户名 3 private String sex;//性别 4 5 /************get/set方法*******************************************/</span> 6 7 publicLong getUid() { 8 returnuid; 9 } 10 publicvoid setUid(Long uid) { 11 this.uid= uid; 12 } 13 publicString getUsername() { 14 returnusername; 15 } 16 publicvoid setUsername(String username) { 17 this.username= username; 18 } 19 publicString getSex() { 20 returnsex; 21 } 22 publicvoid setSex(String sex) { 23 this.sex= sex; 24 }
4,UserAction.java——处理业务,获取用户
1 importcom.opensymphony.xwork2.ActionSupport; 2 3 public classUserAction extends ActionSupport{ 4 privateLong uid; 5 privateString username; 6 privateString password; 7 privateString sex; 8 privateString phone; 9 /** 10 * 获取用户 11 * @return 12 */ 13 publicString showUser(){ 14 User user = new User();//创建一个User对象 15 user.setUid(1L); 16 user.setUsername("学敏"); 17 user.setSex("女"); 18 user.setPassword("123"); 19 user.setPhone("15466554589"); 20 21 this.uid=user.getUid(); 22 this.sex=user.getSex(); 23 this.password=user.getPassword(); 24 this.phone=user.getPhone(); 25 this.username=user.getUsername(); 26 27 returnSUCCESS; 28 } 29 /*******get/set方法**************************/ 30 publicLong getUid() { 31 returnuid; 32 } 33 34 publicvoid setUid(Long uid) { 35 this.uid= uid; 36 } 37 38 publicString getUsername() { 39 returnusername; 40 } 41 42 publicvoid setUsername(String username) { 43 this.username= username; 44 } 45 46 publicString getPassword() { 47 returnpassword; 48 } 49 50 publicvoid setPassword(String password) { 51 this.password= password; 52 } 53 54 publicString getSex() { 55 returnsex; 56 } 57 58 publicvoid setSex(String sex) { 59 this.sex= sex; 60 } 61 62 publicString getPhone() { 63 returnphone; 64 } 65 66 publicvoid setPhone(String phone) { 67 this.phone= phone; 68 } 69 }
5,配置Strut2的配置文件Struts.xml
——继承json-default,指定Action返回的结果集的类型为:json;
1 <struts> 2 <packagenamepackagename="userjson" namespace="/"extends="json-default"> 3 <actionnameactionname="userJSONAction_*" method="{1}"class="cn.itcast.oa0909.struts2.action.UserAction"> 4 <!--指定返回的结果集的类型为:json --> 5 <resulttyperesulttype="json"></result> 6 </action> 7 </package> 8 </struts>
(注:一旦为Action指定了该结果处理类型,JSON插件就会自动将Action里的数据序列化成JSON格式的数据, 并返回给客户端物理视图的JavaScript。简单的说,JSON插件允许我们在JavaScript中异步的调用Action, 而且Action不需要指定视图来显示Action的信息显示。 而是由JSON插件来负责具体将Action里面具体的信息返回给调用页面。)
6,test.html页面
1 <html> 2 <head> 3 <title>tree.html</title> 4 <meta http-equiv="keywords"content="keyword1,keyword2,keyword3"> 5 <meta http-equiv="description"content="this is my page"> 6 <metahttp-equivmetahttp-equiv="content-type" content="text/html;charset=UTF-8"> 7 </head> 8 9 <!--引入js文件 --> 10 <scriptsrcscriptsrc="js/jquery-1.4.2.js"></script> 11 <scriptsrcscriptsrc="js/test.js"></script> 12 13 <body> 14 This is my HTML page. 15 </body> 16 </html>
7,test.js文件
1 //页面加载执行 2 $().ready(function(){ 3 4 load();//调用load()函数 5 6 }); 7 functionload(){ 8 $.post("userJSONAction_showUser.action",null, function(data){ 9 //弹出服务端返回的数据 10 alert("编号:"+data.uid+",姓名:"+data.username+",性别:"+data.sex); 11 12 }); 13 }
8,运行
地址:http://localhost:8080/Struts2+Ajax/test.html
结果:
三,json插件执行原理时序图