天天看點

配置中的參數含義

配置中的參數含義:

root參數用于指定要序列化的根對象,如果省去這一配置,表示要序列化action中的所有屬性

ignoreHierarchy 為false時表示要序列化根對象的所有基類

excludeProperties表示排除的序列化的屬性

includeProperties表示哪些屬性被序列化

Action配置:

<!-- jsonplugin的使用配置 --> 

        <!-- package要繼承json-default 以加載json插件 --> 

        <action name="jsonAct" class="cn.enjoylife.prac.action.JsonAction"> 

            <result type="json"> 

                <!-- root參數用于指定要序列化的根對象,如果省去這一配置,表示要序列化action中的所有屬性 --> 

                <param name="root">map</param> 

                <!-- ignoreHierarchy 為false時表示要序列化根對象的所有基類 --> 

                <param name="ignoreHierarchy">false</param> 

                <!-- 排除hello屬性,使其不被序列化 --> 

                <param name="excludeProperties">hello</param> 

            </result> 

        </action>

JsonAction.java:

public class JsonAction extends ActionSupport { 

    private static final long serialVersionUID = 3870310687165227565L; 

    private int[] ints = { 100, 200 }; 

    private Map<String, Object> map = new HashMap<String, Object>(); 

    private String hello = "helloWorld"; 

    private String username = "evan";//沒有getter方法,不會被序列化  

    private String password = "pwd"; 

    private String validateCode = "123456"; 

    public int[] getInts() { 

        return ints; 

    } 

    public void setInts(int[] ints) { 

        this.ints = ints; 

    } 

    @JSON(name="newMap")//重新命名  

    public Map<String, Object> getMap() { 

        return map; 

    } 

    public void setMap(Map<String, Object> map) { 

        this.map = map; 

    } 

    public String getHello() { 

        return hello; 

    } 

    public void setHello(String hello) { 

        this.hello = hello; 

    } 

    public void setUsername(String username) { 

        this.username = username; 

    } 

    @JSON(serialize=true)//這是預設值,可以省去  

    public String getPassword() { 

        return password; 

    } 

    public void setPassword(String password) { 

        this.password = password; 

    } 

    @JSON(serialize=false)//serialize參數為false,不會被序列化  

    public String getValidateCode() { 

        return validateCode; 

    } 

    public void setValidateCode(String validateCode) { 

        this.validateCode = validateCode; 

    } 

    @Override 

    public String execute() throws Exception { 

        map.put("info", "今天的雨真大啊!"); 

        return SUCCESS; 

    } 

使用jquery操作傳回的json串:

$(document).ready(function(){ 

    $.getJSON( 

        "./jsonAct.action",  

        function(data){ 

            alert(data.info); 

        } 

    ); 

});

Annotation:

  1. package com.zheshou.action;   
  2. import javax.annotation.Resource;   
  3. import org.apache.struts2.convention.annotation.Action;   
  4. import org.apache.struts2.convention.annotation.Namespace;   
  5. import org.apache.struts2.convention.annotation.ParentPackage;   
  6. import org.apache.struts2.convention.annotation.Result;   
  7. import com.opensymphony.xwork2.ActionSupport;   
  8. import com.zheshou.model.Dept;   
  9. import com.zheshou.model.User;   
  10. import com.zheshou.service.DeptService;   
  11. import com.alibaba.fastjson.JSON;   
  12. @ParentPackage(value="json-default")   
  13. @Namespace(value="/")   
  14. public class SelectDeptAction extends ActionSupport {//2.ActionSupport必須有這個才行.但是可以不用Execute了  
  15.     private DeptService deptService;   
  16.     private String beginTime;   
  17.     private String endTime;   
  18.     private String Emps;   
  19.     private String result;   
  20.     public DeptService getDeptService() {   
  21.         return deptService;   
  22.     }   
  23.     @Resource(name="deptService")   
  24.     public void setDeptService(DeptService deptService) {   
  25.         this.deptService = deptService;   
  26.     }      
  27.     public String getBeginTime() {   
  28.         return beginTime;   
  29.     }   
  30.     public void setBeginTime(String beginTime) {   
  31.         this.beginTime = beginTime;   
  32.     }   
  33.     public String getEndTime() {   
  34.         return endTime;   
  35.     }   
  36.     public void setEndTime(String endTime) {   
  37.         this.endTime = endTime;   
  38.     }   
  39.     public String getEmps() {   
  40.         return Emps;   
  41.     }   
  42.     public void setEmps(String emps) {   
  43.         Emps = emps;   
  44.     }   
  45.     public String getResult() {   
  46.         return result;   
  47.     }   
  48.     public void setResult(String result) {   
  49.         this.result = result;   
  50.     }   
  51.     @Action(value="selectdept"  
  52.         ,results={@Result(   
  53.                 type="json"  
  54.                 ,params={"contentType","text/html","root","result"}//root<會帶外引号>    
  55. //includeProperties Result不帶字元引号   
  56. //excludeProperties 傳回所有的屬性   
  57.                     )}   
  58.                )   
  59.     public String execute(){   
  60.         //result= JSON.toJSONString(user).replaceAll("\"", "'");   
  61.         result=deptService.Select(Emps);   
  62.         System.out.print(result);   
  63.         return SUCCESS;   
  64.     }   
  65. }

繼續閱讀