摘要: 主要实现步骤如下: 1、JSP页面使用脚本代码执行ajax请求 2、Action中查询出需要返回的数据,并转换为json类型模式数据 3、配置struts.xml文件 4、页面脚本接受并处理数据 
网上看到很多关于Struts2+ajax+jquery+json的例子,但是很多都不完整,也看不明白,主要原因是返回jsno类型数据和原来的返回字符串类型数据不一样,并且网友们实现步骤没有说清楚,让初学的朋友捉摸不透到底该怎么做。
我做了个简单的demo,供网友们学习,最后我会附上链接,可以下载整个demo.
首先需要的包(struts核心包和json需要的包):
struts核心包:

json需要的包:

commons-logging-*.jar在导入struts核心包的时候就导入了,所以导入json包的时候可以去掉这个包
页面效果:

 
json_demo.jsp页面(该页面引用了jquery文件,我用的版本是jquery-1.8.2.js,如果使用版本不同,请自行修改):
| 01 | <%@ page language="java" contentType="text/html; charset=UTF-8" | 
 
| 02 |     pageEncoding="UTF-8"%> | 
 
| 03 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | 
 
| 06 | <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"> | 
 
| 07 | <title>Simpleton Demo | struts+ajax返回json类型数据</title> | 
 
| 09 | <linkrel="shortcut icon"type="image/x-icon"href="images/Icon.png"/> | 
 
| 10 | <linkrel="stylesheet"type="text/css"href="styles/base.css"/> | 
 
| 13 | <bodybackground="images/bg.gif"> | 
 
| 18 |         <formaction="#"method="post"> | 
 
| 19 |             <labelfor="name">姓名:</label><inputtype="text"name="name"/> | 
 
| 20 |             <labelfor="age">年龄:</label><inputtype="text"name="age"/> | 
 
| 21 |             <labelfor="position">职务:</label><inputtype="text"name="position"/> | 
 
| 22 |             <inputtype="button"class="btn"value="提交结果"/> | 
 
| 28 |             <li>姓名:<spanid="s_name">赞无数据</span></li> | 
 
| 29 |             <liclass="li_layout">年龄:<spanid="s_age">暂无数据</span></li> | 
 
| 30 |             <liclass="li_layout">职务:<spanid="s_position">暂无数据</span></li> | 
 
| 34 |     <divid="authorgraph"><imgalt=""src="images/autograph.gif"></div> | 
 
| 36 |     <scripttype="text/javascript"src="scripts/jquery-1.8.2.js"></script> | 
 
| 37 |     <scripttype="text/javascript"> | 
 
| 42 |             var $btn = $("input.btn");//获取按钮元素 | 
 
| 44 |             $btn.bind("click",function(){ | 
 
| 48 |                     url:"excuteAjaxJsonAction",//需要用来处理ajax请求的action,excuteAjax为处理的方法名,JsonAction为action名 | 
 
| 50 |                         name:$("input[name=name]").val(), | 
 
| 51 |                         age:$("input[name=age]").val(), | 
 
| 52 |                         position:$("input[name=position]").val()//这里不要加","  不然会报错,而且根本不会提示错误地方 | 
 
| 54 |                     dataType:"json",//设置需要返回的数据类型 | 
 
| 55 |                     success:function(data){ | 
 
| 56 |                         var d = eval("("+data+")");//将数据转换成json类型,可以把data用alert()输出出来看看到底是什么样的结构 | 
 
| 57 |                         //得到的d是一个形如{"key":"value","key1":"value1"}的数据类型,然后取值出来 | 
 
| 59 |                         $("#s_name").text(""+d.name+""); | 
 
| 60 |                         $("#s_age").text(""+d.age+""); | 
 
| 61 |                         $("#s_position").text(""+d.position+""); | 
 
| 72 |         $(document).ready(function(){           | 
 
 
 
JsonAction.java代码
| 01 | packagecom.simpleton.demo.action; | 
 
| 03 | importjava.util.HashMap; | 
 
| 06 | importjavax.servlet.http.HttpServletRequest; | 
 
| 08 | importnet.sf.json.JSONObject; | 
 
| 10 | importorg.apache.struts2.interceptor.ServletRequestAware; | 
 
| 12 | importcom.opensymphony.xwork2.ActionSupport; | 
 
| 14 | publicclassJsonAction extendsActionSupport implementsServletRequestAware{ | 
 
| 15 |     privatestaticfinallongserialVersionUID = 1L; | 
 
| 17 |     privateHttpServletRequest request; | 
 
| 18 |     privateString result; | 
 
| 20 |     publicvoidsetServletRequest(HttpServletRequest arg0) { | 
 
| 23 |     publicString getResult() { | 
 
| 26 |     publicvoidsetResult(String result) { | 
 
| 34 |     publicString excuteAjax(){ | 
 
| 38 |             String name = request.getParameter("name"); | 
 
| 39 |             intage = Integer.parseInt(request.getParameter("age")); | 
 
| 40 |             String position = request.getParameter("position"); | 
 
| 43 |             Map<String,Object> map = newHashMap<String,Object>(); | 
 
| 44 |             map.put("name", name); | 
 
| 46 |             map.put("position", position); | 
 
| 48 |             JSONObject json = JSONObject.fromObject(map); | 
 
| 49 |             result = json.toString(); | 
 
| 50 |         } catch(Exception e) { | 
 
 
 
struts.xml中
| 01 | <?xmlversion="1.0"encoding="UTF-8"?> | 
 
| 02 | <!DOCTYPE struts PUBLIC | 
 
| 03 |     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" | 
 
| 04 |     "http://struts.apache.org/dtds/struts-2.0.dtd"> | 
 
| 08 |     <constantname="struts.i18n.encoding"value="UTF-8"></constant> | 
 
| 10 |     <packagename="simpleton"extends="struts-default,json-default"> | 
 
| 12 |         <actionname="*JsonAction"method="{1}"class="com.simpleton.demo.action.JsonAction"> | 
 
| 13 |             <resultname="fail"></result> | 
 
| 16 |                 <paramname="root">result</param> | 
 
 
 
这样就可以完成一个简单json数据类型传递的demo了。
 
下面附上源码文件(百度网盘),基于eclipse开发,导入即可运行:http://pan.baidu.com/share/link?shareid=2994183962&uk=1646424500