Struts2JSON

转载自:
http://code.google.com/p/j2eewiki/wiki/Struts2JSON


1.导入jsonplugin包
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下有)

2.struts.xml中package 中extends="json-default"
<package name="json" namespace="/json" extends="json-default">
3.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属性 -->
<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>
4.避免使用get开头的action方法
在属性get方法上面加
@JSON(name="newName")json中的名称
@JSON(serialize=false) 属性不被加入json
@JSON(format="yyyy-MM-dd") 格式化日期
5.在action中赋值,返回对应的result字符串

猜你喜欢

转载自shenjc2008.iteye.com/blog/1424200
今日推荐