struts2 json插件

1struts2 json插件 - 冬 - ysd-spring 的博客 <packagename="default"extends="struts-default">

改为

1struts2 json插件 - 冬 - ysd-spring 的博客 <packagename="default"extends="json-default">

4.Action中的配置

1struts2 json插件 - 冬 - ysd-spring 的博客 <actionname="testAction"class="com.json.action.TestAction"method="testMethod">

2struts2 json插件 - 冬 - ysd-spring 的博客 <interceptor-refname="json"/><!--处理以JSON文本提交的请求-->

3struts2 json插件 - 冬 - ysd-spring 的博客 <resulttype="json"/><!--将action的bean属性以json字符串返回浏览器-->

4struts2 json插件 - 冬 - ysd-spring 的博客 </action>

<action name="testAction" class="com.json.action.TestAction" method="testMethod"><interceptor-ref name="json" /><!--处理以JSON文本提交的请求--><result type="json" /> <!--将action的bean属性以json字符串返回浏览器--></action>

注:根据需要选择interceptor和result配置

5. 你会发现前台返回的json字符串,是把action中的所有属性全部转化为json字符串返回给浏览器了,但是我有时候需要根据实际情况返回部分结果,如何对json的结果进行定制输出呢?result提供了一些参数替你解决这个问题

5.1. root参数:从返回结果中根据ognl表达式取出你需要输出的结果

如:

action类

struts2 json插件 - 冬 - ysd-spring 的博客struts2 json插件 - 冬 - ysd-spring 的博客 publicclassBaseActionextendsActionSupportimplementsstruts2 json插件 - 冬 - ysd-spring 的博客 {

struts2 json插件 - 冬 - ysd-spring 的博客 privatePersonperson=null;

struts2 json插件 - 冬 - ysd-spring 的博客struts2 json插件 - 冬 - ysd-spring 的博客

struts2 json插件 - 冬 - ysd-spring 的博客 }

public class BaseAction extends ActionSupport implements {private Person person = null;...}

bean类

1struts2 json插件 - 冬 - ysd-spring 的博客struts2 json插件 - 冬 - ysd-spring 的博客 publicclassPersonstruts2 json插件 - 冬 - ysd-spring 的博客 {

2struts2 json插件 - 冬 - ysd-spring 的博客 privateStringname;

3struts2 json插件 - 冬 - ysd-spring 的博客 privateintage;

4struts2 json插件 - 冬 - ysd-spring 的博客struts2 json插件 - 冬 - ysd-spring 的博客

5struts2 json插件 - 冬 - ysd-spring 的博客 }

public class Person {private String name;private int age;...}

我们只要输出person对象的name属性值,配置如下

1struts2 json插件 - 冬 - ysd-spring 的博客 <resulttype="json">

2struts2 json插件 - 冬 - ysd-spring 的博客 <paramname="root">person.name</param>

3struts2 json插件 - 冬 - ysd-spring 的博客 </result>

<result type="json"><param name="root">person.name</param></result>

5.2. excludeNullProperties 参数:表示是否去掉空值,默认值是false,如果设置为true会自动将为空的值过滤,只输出不为空的值。

1struts2 json插件 - 冬 - ysd-spring 的博客 <resulttype="json">

2struts2 json插件 - 冬 - ysd-spring 的博客 <paramname="excludeNullProperties">true</param>

3struts2 json插件 - 冬 - ysd-spring 的博客 </result>

4struts2 json插件 - 冬 - ysd-spring 的博客

<result type="json"><param name="excludeNullProperties">true</param></result>

5.3. ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction继承于BaseAction,那么TestAction中返回的json字符串默认 是不会包含父类BaseAction的属性值,ignoreHierarchy值默认为true,设置为false后会将父类和子类的属性一起返回。

1struts2 json插件 - 冬 - ysd-spring 的博客 <resulttype="json">

2struts2 json插件 - 冬 - ysd-spring 的博客 <paramname="ignoreHierarchy">false</param>

3struts2 json插件 - 冬 - ysd-spring 的博客 </result>

<result type="json"><param name="ignoreHierarchy">false</param></result>

5.4. includeProperties 参数:输出结果中需要包含的属性值,这里正则表达式和属性名匹配,可以用“,”分割填充多个正则表达式。

如:输出person的所有属性

1struts2 json插件 - 冬 - ysd-spring 的博客 <resulttype="json">

2struts2 json插件 - 冬 - ysd-spring 的博客 <paramname="includeProperties">person.*,person\.name</param>

3struts2 json插件 - 冬 - ysd-spring 的博客 </result>

<result type="json"><param name="includeProperties">person.*, person\.name</param></result>

5.5. excludeProperties 参数:输出结果需要剔除的属性值,也支持正则表达式匹配属性名,可以用“,”分割填充多个正则表达式,类同4.4.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值