public class JSONExample { //封装请求参数的三个属性 private String field1; private transient String field2; private String field3; //封装处理结果的属性 private int[] ints = {10, 20}; private Map map = new HashMap(); private String customName = "custom"; //三个请求参数对应的setter和getter方法 public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } //此处省略了field1和field2两个字段的setter和getter方法 ... //封装处理结果的属性的setter和getter方法 public int[] getInts() { return ints; } public void setInts(int[] ints) { this.ints = ints; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } //使用注释语法来改变该属性序列化后的属性名 @JSON(name="newName") public String getCustomName() { return this.customName; } public String execute() { map.put("name", "yeeku"); return Action.SUCCESS; } } |
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"/> <package name="example" extends="json-default"> <action name="JSONExample" class="lee.JSONExample"> <result type="json"/> </action> </package> </struts> |