action向页面传递数据,拦截那些没必要传到客户端的方法。includeProperties

如果action的属性很多,我们想要从Action返回到调用页面的数据。这个时候配置includeProperties或者 excludeProperties拦截器即可。而这2个拦截器的定义都在struts2的json-default包内,所以要使用该拦截器的包都要继承自json-default。

01
02
03
04
05
06
07
08
09
10
11
< struts >
     < constant value = "spring" name = "struts.objectFactory" />
     < include file = "struts-admin.xml" ></ include >
     < package name = "default" extends = "json-default" >
         < action class = "com.person.PersonAction" name = "person" method = "view" >
             < result type = "json" >
                 < param name = "includeProperties" >person\.name,persoon\.age,person\.gender </ param >
             </ result >
         </ action >
     </ package >
</ struts >


经过测试,下面的设置也是可以的,就是在includeProperties里面直接写你所在Action的属性

01
02
03
04
05
06
07
08
09
10
11
< struts >
     < constant value = "spring" name = "struts.objectFactory" />
     < include file = "struts-admin.xml" ></ include >
     < package name = "default" extends = "json-default" >
         < action class = "com.person.PersonAction" name = "person" method = "view" >
             < result type = "json" >
                 < param name = "includeProperties" >name,age,gender </ param >
             </ result >
         </ action >
     </ package >
</ struts >

利用Struts 2的支持的可配置结果,可以达到过滤器的效果。Action的处理结果配置支持正则表达式。但是如果返回的对象是一个数组格式的Json数据。比如 peson Bean中有对象persion1…person9,而我只要JSON插件,则可以用如下的正则表达式。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
< struts >
     < constant value = "spring" name = "struts.objectFactory" />
     < include file = "struts-admin.xml" ></ include >
     < package name = "default" extends = "json-default" >
         < action class = "com.person.PersonAction" name = "person" method = "view" >
             < result type = "json" >
                 < param name = "includeProperties" >person\.name,persoon\.age,person\.gender </ param >
             </ result >
         </ action >
     </ package >
</ struts >
<!--利用Struts 2的支持的可配置结果,可以达到过滤器的效果。Action的处理结果配置支持正则表达式。 但是如果返回的对象是一个数组格式的Json数据。比如peson Bean中有对象persion1...person9,而我只要person1的json数据, 则可以用如下的正则表达式。 -->
< struts >
     < constant value = "spring" name = "struts.objectFactory" />
     < include file = "struts-admin.xml" ></ include >
     < package name = "default" extends = "json-default" >
         < action class = "com.person.PersonAction" name = "person" method = "view" >
             < result type = "json" >
                 < param name = "includeProperties" >person\[\d+\]\.person1 </ param >
             </ result >
         </ action >
     </ package >
</ struts >
<!--excludeProperties拦截器的用法与此类同,如果拦截的仅仅是一个对象,如果拦截掉person Bean的整个对象。 -->
< struts >
     < constant value = "spring" name = "struts.objectFactory" />
     < include file = "struts-admin.xml" ></ include >
     < package name = "default" extends = "json-default" >
         < action class = "com.person.PersonAction" name = "person" method = "view" >
             < result type = "json" >
                 < param name = "excludeProperties" >person </ param >
             </ result >
         </ action >
     </ package >
</ struts >

     传递List或者对象是,后面要加上  .*   代表全部的意思。

<param name="includeProperties">topicList.*</param> 


需要注意的是,如果用JSON插件把返回结果定为JSON。而JSON的原理是在ACTION中的get方法都会序列化,所以前面是get的方法只要没指定不序列化,都会执行,那么可以在该方法的前面加注解声明该方法不做序列化。

1
2
3
4
5
6
7
8
9
@JSON (serialize = false )
public User getUser() {
     return this .User;
}
 
@JSON (format = "yyyy-MM-dd" )
public Date getStartDate() {
     return this .startDate;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值