来源:http://www.blogjava.net/yangjunwei/articles/377102.html
1,添加jar包struts2-json-plugin-2.3.1.2.jar
2.在json-struts.xml里配置
<package name="myjson" extends="json-default">
<action name="myjson" class="com.yjw.web.MyjsonAction">
<result type="json">
<!--找到根节点-->
<param name="root">user</param>
<!--浏览器不要缓存-->
<param name="noCache">true</param>
<!--GZIP网页压缩协议,可以让传送更快,省流量-->
<param name="enableGZIP">true</param>
<!--排除action里是null的属性-->
<param name="excludeNullProperties">true</param>
</result>
</action>
</package>
在MyjsonAction里写:
package com.yjw.web;
import com.opensymphony.xwork2.Action;
public class MyjsonAction implements Action {
private User user;
private String x;
public String execute() throws Exception {
user = new User();
user.setId(1);
user.setMoney(22);
user.setName("tom");
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
}