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;
}

你可能感兴趣的:(action向页面传递数据,拦截那些没必要传到客户端的方法。includeProperties)