`

Struts2 入门 五 (struts2 关于JSON )

 
阅读更多

 

Struts2 入门 五 (struts2 关于JSON )

 

觉得JSON有必要简单说下 异步交互的时候 经常要用到的

尤其是JSONObject 和JSONArray 对象

其实把见好多人Struts.xml中配置这样写如下:

<result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
  </result-types>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>


struts2-json-plugin-2.1.8.1.jar json-lib-2.1.jar commons-collections-3.1.jar
commons-beanutils-1.7.0.jar ezmorph-1.0.3
commons-logging.jar


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

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

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

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

03.root 参数:从返回结果中根据 ognl 表达式取出你需要输出的结果。
只输出 person 对象的 name 属性值,配置如下:

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


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

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


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

 

其实吧 要我来说真心麻烦,如果Servlet原理你懂的话 那么的话 不用这样写照样是能够实现的 上面太繁琐了 还要记那么多属性。

Struts.xml中可以这样写:

 

<action name="index" class="" method=""></action>

主要是方法里面 其实吧 就是用了 代码贴上:

	//建议吧json对象或者数据对象转化成字符串回写回去
       ServletActionContext.getResponse().getWriter().print("一样");
      ServletActionContext.getResponse().getWriter().write("JSON对象或者JSON数组对象");

就是这样简单方便 Struts2 底层框架还是依赖的Servlet。 没什么区别 觉得 只不过比Servlet更方便了些.



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics