问题:
如何指定struts2返回我们需要的json类型数据?
--------------------------------------------------------------------------------------------------
解决方案:
在struts2.xml配置文件中里配置
<param name="includeProperties"></param> 这个属性表示要包含进JSON数据中的数据。
<param name="excludeProperties"></param> 这个属性表示不要包含进JSON数据中的数据。
注意:Jar包是jsonplugin-0.25的,只支持excludeProperties,不支持includeProperties。从0.28版本才开始支持includeProperties
讨论:
只要有get方法,都会包含到json对象的属性,比如此useraction转为json如下:
{"allDatas":null,"analysisList":null,"maps":{6388:170},"message":null,"model":{"attachName":null,"attachPath":null,"auditStatus":null,"briefTitle":"null"}"
有时候我们只想指定的字段转为json,这时需要在xml里配置<param name="includeProperties">。
xml配置
1 <package name="example" extends="json-default">2 <action name="JSONExample" class="com.test.UserAction">3 <result type="json">4 <param name="includeProperties">5 name,maps.*6 </param>7 </result>8 </action>9 </package>
配置返回refreshDatas集合中的字段
1 <action name="vote" class="com.test.VoteAction" method="{1}">2 <result name="error" type="json">3 <param name="includeProperties">message</param>4 </result>5 <result name="success" type="json">6 <param name="includeProperties">message,refreshDatas\[\d+\]\.newsId,refreshDatas\[\d+\]\.numberOfVotes</param>7 </result>8 </action>
=================================================================================================
参考文档:
http://bosslife.blog.163.com/blog/static/114917017201061911130625/
http://bangsen.iteye.com/blog/969353
http://topic.csdn.net/u/20091214/14/c57aa29d-2588-473c-8e17-032e35a8e696.html
http://wenku.baidu.com/view/26d9a9f67c1cfad6195fa734.html
http://struts.apache.org/2.2.3/docs/json-plugin.html