2013-03-20 53 views
1

我用struts-JSON-plugin.2.2.3的操作,其結果類型是JSON,這裏是一個演示配置:有沒有一種方便的方式來爲struts2中的json結果配置noCache和excludeNullProperties參數?

<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信息響應標題和空屬性發送到瀏覽器。 那爲什麼它不起作用?
如果有一種方便的方法來配置這兩個參數?

+0

的' 「JSON」'攔截器的配置不會影響' 「JSON」'結果的配置。我不認爲有一種方法可以在全局範圍內設置結果參數,但我可能會誤解。最方便的方法可能是隻取現有的json結果,擴展它,並定義你自己的結果類型。 – 2013-03-20 12:45:27

回答

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

太棒了,它的作品,非常感謝。 – hiway 2013-03-21 02:01:47

相關問題