若是action的属性不少,咱们想要从Action返回到调用页面的数据。这个时候配置includeProperties或者 excludeProperties拦截器便可。而这2个拦截器的定义都在struts2的json-default包内,因此要使用该拦截器的包都要继承自json-default。java
xml文件:ajax
<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,person\.age,person\.gender </param> </result> </action> </package> </struts>
通过测试,下面的设置也是能够的,就是在includeProperties里面直接写你所在Action的属性正则表达式
<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,而我只要persion1,则能够用以下的正则表达式。spring
<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的整个对象,写法以下: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="excludeProperties">person </param> </result> </action> </package> </struts>
传递List或者对象是,后面要加上 .* 表明所有的意思。数组
<param name="includeProperties">topicList.*</param>
须要注意的是,若是用JSON插件把返回结果定为JSON。而JSON的原理是在ACTION中的get方法都会序列化,因此前面是get的方法只要没指定不序列化,都会执行,那么能够在该方法的前面加注解声明该方法不作序列化。测试
@JSON(serialize = false) public User getUser() { return this.User; } @JSON(format = "yyyy-MM-dd") public Date getStartDate() { return this.startDate; }
项目中遇到的问题:this
strut2提供了支持json的插件spa
必须继承json-default、json-default继承自struts-default.插件
<package name="json" namespace="/" extends="json-default"> <action name="moduleAction" class="moduleAction" > <result type="json" name="findData"> <param name="root">datas</param> <param name="includeProperties">success,totalcount</param> </result> <result type="json" name="findTree"> <param name="root">result</param> </result> </action> </package>
1,result中type设置成json以后,容器会把action的属性自动封装到一个json对象中(json拦截器来作),而后调用ajax的callback方法. 返回json数据
2,includeProperties 参数:输出结果中须要包含的属性值,这里正则表达式和属性名匹配,能够用“,”分割填充多个正则表达式。 如<param name="includeProperties">module.*,user\.userName</param>? 表示是module的全部属性及用户的用户名
3,excludeProperties 参数:输出结果须要排除的属性值,也支持正则表达式匹配属性名,能够用“,”分割填充多个正则表达式,类同includeProperties
4,为何要用includeProperties或excludeProperties 参数:
a.主要是为了过滤掉接口,pojo的set、list、其它对象等不须要的数据防止循环取其它关联对象或找不到接口。
b.若是不配置,默认是处理 action中的全部属性,若是action中有接口注入,json拦截器可能找不到接口而返回不告终果,
c.还有若是action中有一个对象,这个对象与好多对象都有关联,json拦截器会将相关联的全部对象的属性所有转换成json格式,
d. 若是其它对象有list、set,其返回结果至关庞大,有多是死循环而没法返回 。
e.若是不用<param name="includeProperties">或其余方式进行json数据过滤,经过debug你会发现前台返回的json字符串,是把 action中的全部属性所有转化成json字符串返回给客户端(包括service接口、pojo全部属性及有关联的pojo。有时候根本返回不告终果,也不报错,后台虽然执行了, 但前台执行不到callback function,这主要是由于找不到接口或者关联的pojo太多,形成死循环),
f. 通常状况下用的最多的就是root、 includeProperties 和excludeNullProperties参数。
g. 固然还有其余的方法,如给pojo的属性加json注解。
5,result标签中的name属性,即表示是struts2中的action类中返回的名称
如:
public String findAllById() throws Exception{ int id=Integer.parseInt( this.request.getParameter("pid")); List<ModuleBean> list=service.findById(id); return "findTree"; }
上面代码return "findTree"就会找到findTree的结果集,并将其封装起来,返回json给客户端
所以 action 能够配置多个result