配置中的参数含义

配置中的参数含义:

 

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:

 

/**

 * @author Evan

 * 

 */ 

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值