是否有方便的方法为struts2中的json结果配置noCache和excludeNullProperties参数?

时间:2013-03-20 10:41:29

标签: json struts2

我对结果类型为json的Actions使用struts-json-plugin.2.2.3,这是一个演示配置:

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

此配置正常。但对于我的项目中的所有操作,noCacheexcludeNullProperties具有与上面的代码相同的配置值,因此我正在寻找一种方法在一个地方配置它们并用于所有。我在JSONInterceptor类中找到了相同的名称属性,所以我这样配置:

  <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-controlexpirespragma信息,并且属性为null被发送到浏览器。 那为什么它不起作用?
如果有方便的方法来配置这两个参数?

1 个答案:

答案 0 :(得分:1)

result-type文件中尝试此struts.xml配置:

<result-type name="json" class="org.apache.struts2.json.JSONResult">
  <param name="noCache">true</param>
  <param name="excludeNullProperties">true</param>
</result-type>