Struts2JSON

http://code.google.com/p/j2eewiki/wiki/Struts2JSON
Struts2JSON  
Struts2返回JSON
Updated May 10, 2010 by cm2...@gmail.com

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字符串

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值