`
beyond429
  • 浏览: 93377 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Struts2返回JSON对象总结

阅读更多
Struts2返回JSON对象总结 (转)
转载自 日暮月垂

1.struts.xml中package 中extends="json-default"


<package name="json" namespace="/json" extends="json-default"> 
<package name="json" namespace="/json" extends="json-default">


2.result 中type="json"


<!-- 封装所有的get开头的方法 -->  
<result type="json" name="user">  
</result>  
 
<!-- 只包含user.id属性 -->  
<result type="json" name="user">  
    <param name="includeProperties">                 
        user\.id  
    </param>  
</result>  
 
<!-- 返回user的JSON List列表,其中userInfosList是action中的一个List类型的属性,userInfosList \[\d+\]\. userName表示,userInfosList中存储的对象0..end的userName属性(list中存储的对象必须有userName属性)  
-->  
<result  name="success" type="json">  
<param name="includeProperties">  
    userInfosList\[\d+\]\.userName,userInfosList\[\d+\]\.password  
</param>  
</result>  
 
<!-- 不包含user属性 -->  
<result type="json" name="list">  
    <param name="excludeProperties">                 
        user  
    </param>  
</result>  
 
<!-- 根对象只包含user -->  
<result type="json">    
    <param name="root">    
        user  
    </param>    
</result>   
 
<!-- "root"对象中父类的field(属性)不会(会?) 默认存放到 JSON数据中,如果不想这样做,需要在配置时指定 ignoreHierarchy 为 false:  -->  
<result type="json">    
    <param name="ignoreHierarchy">false</param>    
</result>   
   
<!-- 封装所有的get开头的方法 --><result type="json" name="user"></result><!-- 只包含user.id属性 --><result type="json" name="user">    <param name="includeProperties">                       user\.id    </param></result><!-- 返回user的JSON List列表,其中userInfosList是action中的一个List类型的属性,userInfosList \[\d+\]\. userName表示,userInfosList中存储的对象0..end的userName属性(list中存储的对象必须有userName属性)--><result  name="success" type="json"><param name="includeProperties"> userInfosList\[\d+\]\.userName,userInfosList\[\d+\]\.password</param></result><!-- 不包含user属性 --><result type="json" name="list">    <param name="excludeProperties">                       user    </param></result><!-- 根对象只包含user --><result type="json">      <param name="root">          user    </param>  </result> <!-- "root"对象中父类的field(属性)不会(会?) 默认存放到 JSON数据中,如果不想这样做,需要在配置时指定 ignoreHierarchy 为 false:  --><result type="json">      <param name="ignoreHierarchy">false</param>  </result>  


3.避免使用get开头的action方法
  在属性get方法上面加
  @JSON(name="newName")json中的名称
  @JSON(serialize=false) 该属性不被加入json
  @JSON(format="yyyy-MM-dd") 格式化日期

4.在action中赋值,返回对应的result字符串

说明:
为什么要用includeProperties或excludeProperties 参数:主要是为了过滤掉接口,pojo的set、list、其它对象等不需要的数据防止循环取其它关联对象或找不到接口。如果不配置,默认是处理action中的所有属性,如果action中有接口注入,json拦截器可能找不到接口而返回不了结果,还有如果action中有一个对象,这个对象与好多对象都有关联,json拦截器会将相关联的所有对象的属性全部转换成json格式,如果其它对象有list、set,其返回结果相当庞大,有可能是死循环而无法返回 。如果不用<param name="includeProperties">或其他方式进行json数据过滤,通过debug你会发现前台返回的json字符串,是把action中的所有属性全部转化成json字符串返回给客户端(包括service接口、pojo所有属性及有关联的pojo。有时候根本返回不了结果,也不报错,后台执行了,但前台执行不到callback function,这主要是因为找不到接口或者关联的pojo太多,造成死循环),一般情况下用的最多的就是root、includeProperties 和excludeNullProperties参数。当然还有其他的方法,如给pojo的属性加json注解。

Excluding properties
Xml代码 
<!-- Result fragment --> 
<result type="json"> 
  <param name="excludeProperties"> 
    login.password, 
    studentList.*\.sin 
  </param> 
</result> 
 
<!-- Interceptor fragment --> 
<interceptor-ref name="json"> 
  <param name="enableSMD">true</param> 
  <param name="excludeProperties"> 
    login.password, 
    studentList.*\.sin 
  </param> 
</interceptor-ref> 

Including properties
Xml代码 
<!-- Result fragment --> 
<result type="json"> 
  <param name="includeProperties"> 
    ^entries\[\d+\]\.clientNumber, 
    ^entries\[\d+\]\.scheduleNumber, 
    ^entries\[\d+\]\.createUserId 
  </param> 
</result> 

对集合 listAttachment.*, listAttachment\[\d+\] 含义是不同的
listAttachment.*, 指listAttachment集合对象本身
listAttachment\[\d+\] 指listAttachment集合对象中的元素

总结:
action中避免使用get开头的action方法,去掉action中的接口的get方法。 为json类型的result配置includeProperties, excludeProperties等参数.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics