问题描述
我正在将struts-json-plugin.2.2.3用于结果类型为json的操作,这是一个演示配置:
I am using struts-json-plugin.2.2.3 for Actions whose result type is json, here is an demo configuration:
<action name="dept_*" class="com.XXX.action.DepartmentAction" method="{1}">
<result name="search">dept_search.jsp</result>
<result name="search_ajax" type="json"><param name="root">deptList</param><param name="excludeNullProperties">true</param><param name="noCache">true</param></result>
<result name="save" type="json"><param name="root">jsonResult</param><param name="excludeNullProperties">true</param><param name="noCache">true</param></result>
<result name="count" type="json"><param name="root">pageCount</param><param name="excludeNullProperties">true</param><param name="noCache">true</param></result>
</action>
此配置工作正常.但是对于我项目中的所有Action,noCache
和excludeNullProperties
都具有与上面的代码相同的配置值,因此我正在寻找一种将它们配置在一个地方并用于所有地方的方法.我在JSONInterceptor
类中找到了相同的名称属性,因此我将其配置为:
This configuration works fine. But for all Actions in my project, noCache
and excludeNullProperties
have same configuration value just like code above, so I am searching a way to configure them in one place and used for all. I find in JSONInterceptor
class, there are same name properties, so I configured like this:
<interceptors>
<interceptor-stack name="ecsStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="json"><param name="noCache">true</param><param name="excludeNullProperties">true</param><param name="contentType">application/json;charset=utf-8</param></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="ecsStack"></default-interceptor-ref>
并删除Action result
中的相同配置,但是不能按预期工作,响应标头中没有cache-control
,expires
和pragma
信息,并且将null属性发送到浏览器.那么为什么它不起作用?
是否有方便的方法来配置这两个参数?
And remove same configurations in Action result
, but it does not work as expected, there is no cache-control
, expires
and pragma
information in response headers, and null properties are sent to browser.So why it does not work?
If there is a convenient way to configure these two parameters?
推荐答案
在struts.xml
文件中尝试以下result-type
配置:
Try this result-type
configuration in struts.xml
file:
<result-type name="json" class="org.apache.struts2.json.JSONResult">
<param name="noCache">true</param>
<param name="excludeNullProperties">true</param>
</result-type>
这篇关于是否有一种方便的方法来为struts2中的json结果配置noCache和excludeNullProperties参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!