struts2.1.8以后,加入了json的plugin插件包,使用时仅需把插件包(struts2-json-plugin-2.2.1.1.jar)放入到项目中的WEB-INF/lib下面就可以。这里重点解释返回结果result中的root参数的含义,相对于其他参数,例如includeProperties,excludeProperties,excludeNullProperties等,比较难以理解。
(1)struts.xml 配置:
(2)Action类:类中有一个String类型的属性message,以及一个bean叫UserInfo
public class JsonJqueryStruts2Action extendsActionSupport {
private String message; //使用json返回单个值
private UserInfo userInfo; //使用json返回对象
… …
(3)表单:form中必须与Action类中的属性名字大小写完全对应,不然会找不到该字段
<form>
用户ID:<input name=”userInfo.userId”type=”text”/><br/>
用户名:<input name=”userInfo.userName”type=”text”/><br/>
… …
(4)json.js文件的ajax方法:
//向服务器发送表达数据
$(“#regRe”).click(function(){
//把表单的数据进行序列化
var params = $(“form”).serialize();
对于中文,注意乱码的处理,前面已经有文章说过了。
//使用jQuery中的$.ajax({});Ajax方法
以上是jquery+json+struts2的主要核心配置和代码,请注意struts2配置中指定了result的name=”root”,所谓root的含义,是指返回的json数据的根为root中指定的bean类,例如此处为userInfo,如果没有指定root属性,则默认使用Action作为返回的json数据的根,区别如下:
result中使用了root参数后返回的json数据:
{“userInfo”:[
{"userId":"Patrick","userName":"123456"}
]}
result中没设置root参数返回Action中的json数据:
{“data”:[
{"userInfo":[{"userId":"Patrick","userName":"123456"} ] },
{“message”:”testMesssageData”}
]}
所以我们要注意,如果设置了root属性之后,<param includeProperties></param>中就应该直接使用userId,userName而非userInfo.userId,userIndo.userName了。
此外,json.js代码中还展示了遇到类似message等属性返回后是undefined的情况可以使用typeof()!=’undefined’进行判断
xx表示返回的json对象,root表示返回对象的层级为根部,,因为一般对象可以直接获得更多的对象,比如xx.getParent() 程序会默认以为你要返回的json类型为该对象下的所有属性及对象,以及对象的对象,对象的属性,,,一直下去。
所以如果不指定name="root" 这就会导致死循环,内存溢出了。。。
(1)struts.xml 配置:
<struts>
<constant name=”struts.i18n.encoding”value=”UTF-8″ />
<package name=”default” namespace=”/”extends=”json-default”>
<action name=”jsontest”class=”com.test.action.JsonJqueryStruts2Action”>
<result name=”regInfo” type=”json”>
<param name=”root”>userInfo</param>
<!– <paramname=”includeProperties”>userInfo.userId,userInfo.userName</param>–>
<paramname=”includeProperties”>userId,userName</param>
<paramname=”excludeNullProperties”>true</param>
</result>
</package>
</struts>
(2)Action类:类中有一个String类型的属性message,以及一个bean叫UserInfo
public class JsonJqueryStruts2Action extendsActionSupport {
private String message; //使用json返回单个值
private UserInfo userInfo; //使用json返回对象
… …
(3)表单:form中必须与Action类中的属性名字大小写完全对应,不然会找不到该字段
<form>
用户ID:<input name=”userInfo.userId”type=”text”/><br/>
用户名:<input name=”userInfo.userName”type=”text”/><br/>
… …
(4)json.js文件的ajax方法:
//向服务器发送表达数据
$(“#regRe”).click(function(){
//把表单的数据进行序列化
var params = $(“form”).serialize();
对于中文,注意乱码的处理,前面已经有文章说过了。
//使用jQuery中的$.ajax({});Ajax方法
$.ajax({
url:”jsontest!regUserInfo.action”,
type:”POST”,
data:params,
dataType:”json”,
success:function(data){
//清空显示层中的数据
$(“#message”).html(“”);
//为显示层添加获取到的数据
//获取对象的数据用data.userInfo.属性
//$(“#message”).append(“<div><fontcolor=’red’>用户ID:”+data.userInfo.userId+”</font></div>”)
//.append(“<div><font color=’red’>用户名:”+data.userInfo.userName+”</font></div>”)
$(“#message”).append(“<div><fontcolor=’greed’>111:”+data.userId+”</font></div>”);
$(“#message”).append(“<div><fontcolor=’greed’>222:”+data.userName+”</font></div>”);
if( typeof(data.message)!=’undefined’ ){
$(“#message”).append(“<div><fontcolor=’greed’>结果:”+data.message+”</font></div>”);
}else{
$(“#message”).append(“<div><fontcolor=’greed’>结果是message没定义!”+data.message+”</font></div>”);
}
… …
以上是jquery+json+struts2的主要核心配置和代码,请注意struts2配置中指定了result的name=”root”,所谓root的含义,是指返回的json数据的根为root中指定的bean类,例如此处为userInfo,如果没有指定root属性,则默认使用Action作为返回的json数据的根,区别如下:
result中使用了root参数后返回的json数据:
{“userInfo”:[
{"userId":"Patrick","userName":"123456"}
]}
result中没设置root参数返回Action中的json数据:
{“data”:[
{"userInfo":[{"userId":"Patrick","userName":"123456"} ] },
{“message”:”testMesssageData”}
]}
所以我们要注意,如果设置了root属性之后,<param includeProperties></param>中就应该直接使用userId,userName而非userInfo.userId,userIndo.userName了。
此外,json.js代码中还展示了遇到类似message等属性返回后是undefined的情况可以使用typeof()!=’undefined’进行判断
xx表示返回的json对象,root表示返回对象的层级为根部,,因为一般对象可以直接获得更多的对象,比如xx.getParent() 程序会默认以为你要返回的json类型为该对象下的所有属性及对象,以及对象的对象,对象的属性,,,一直下去。
所以如果不指定name="root" 这就会导致死循环,内存溢出了。。。