首页 > 技术文章 > struts2 使用jsonplugin

hy928302776 2013-10-08 16:23 原文

配置中的参数含义:

root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性 
ignoreHierarchy 为false时表示要序列化根对象的所有基类 
excludeProperties表示排除的序列化的属性

includeProperties表示哪些属性被序列化

Action配置:

 

[xhtml] view plaincopy
  1. <!-- jsonplugin的使用配置 -->  
  2.         <!-- package要继承json-default 以加载json插件 -->  
  3.         <action name="jsonAct" class="cn.enjoylife.prac.action.JsonAction">  
  4.             <result type="json">  
  5.                 <!-- root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性 -->  
  6.                 <param name="root">map</param>  
  7.                 <!-- ignoreHierarchy 为false时表示要序列化根对象的所有基类 -->  
  8.                 <param name="ignoreHierarchy">false</param>  
  9.                 <!-- 排除hello属性,使其不被序列化 -->  
  10.                 <param name="excludeProperties">hello</param>  
  11.             </result>  
  12.         </action>  

 

JsonAction.java:

 

  1. /** 
  2.  * @author Evan 
  3.  *  
  4.  */  
  5. public class JsonAction extends ActionSupport {  
  6.     private static final long serialVersionUID = 3870310687165227565L;  
  7.     private int[] ints = { 100200 };  
  8.     private Map<String, Object> map = new HashMap<String, Object>();  
  9.     private String hello = "helloWorld";  
  10.     private String username = "evan";//没有getter方法,不会被序列化  
  11.     private String password = "pwd";  
  12.     private String validateCode = "123456";  
  13.     public int[] getInts() {  
  14.         return ints;  
  15.     }  
  16.     public void setInts(int[] ints) {  
  17.         this.ints = ints;  
  18.     }  
  19.     @JSON(name="newMap")//重新命名  
  20.     public Map<String, Object> getMap() {  
  21.         return map;  
  22.     }  
  23.     public void setMap(Map<String, Object> map) {  
  24.         this.map = map;  
  25.     }  
  26.     public String getHello() {  
  27.         return hello;  
  28.     }  
  29.     public void setHello(String hello) {  
  30.         this.hello = hello;  
  31.     }  
  32.     public void setUsername(String username) {  
  33.         this.username = username;  
  34.     }  
  35.     @JSON(serialize=true)//这是默认值,可以省去  
  36.     public String getPassword() {  
  37.         return password;  
  38.     }  
  39.     public void setPassword(String password) {  
  40.         this.password = password;  
  41.     }  
  42.     @JSON(serialize=false)//serialize参数为false,不会被序列化  
  43.     public String getValidateCode() {  
  44.         return validateCode;  
  45.     }  
  46.     public void setValidateCode(String validateCode) {  
  47.         this.validateCode = validateCode;  
  48.     }  
  49.     @Override  
  50.     public String execute() throws Exception {  
  51.         map.put("info""今天的雨真大啊!");  
  52.         return SUCCESS;  
  53.     }  
  54. }  

 

使用jquery操作返回的json串:

 

[javascript] view plaincopy
  1. $(document).ready(function(){  
  2.     $.getJSON(  
  3.         "./jsonAct.action",   
  4.         function(data){  
  5.             alert(data.info);  
  6.         }  
  7.     );  
  8. });  

 

https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin

推荐阅读