Struts2.16:导入jsonplugin-0.34.jar包(下载包)和commons-logging-1.0.4.jar(Struts2 lib下有)
Struts2.18导入struts2-json-plugin-2.1.8.1.jar(Struts2 lib下有)
<package name="json" namespace="/json" extends="json-default">
<!-- 封装所以的get开头的方法 -->
<result type="json" name="user"> </result>
<!-- 只包含user.id属性 -->
<result type="json" name="user"> <param name="includeProperties"> user\.id </param> </result>
<!-- 不包含user属性 -->
<result type="json" name="list"> <param name="excludeProperties"> user </param> </result>
<!-- 根对象只包含user -->
<result type="json"> <param name="root"> user </param> </result>
===========如下=========
Struts.xml代码:
<action name="getCoalSuperviseJson" class="com.coal.action.CoalSuperviseAction" method="getCoalSuperviseJson"> <result type="json"> <param name="root">coalSuperviseListMapJSON</param><!--注意这里--> </result> </action>
===========
Action代码:
public String getCoalSuperviseJson() { try { if (coalSuperviseMsg == null) { coalSuperviseMsg = new CoalSuperviseMsg(); } Object result = coalSuperviseService.getCoalSuperviseMap(coalSuperviseMsg, page, rows, sort, order); coalSuperviseListMapJSON = JSONObject.fromObject(result); //注意这里 } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); } return SUCCESS; }
===========
<!-- "root"对象中父类的field(属性)不会(会?) 默认存放到 JSON数据中,如果不想这样做,需要在配置时指定 ignoreHierarchy 为 false: -->
<result type="json"> <param name="ignoreHierarchy">false</param> </result>
在属性get方法上面加
@JSON(name="newName")json中的名称
@JSON(serialize=false) 属性不被加入json
@JSON(format="yyyy-MM-dd") 格式化日期