我試圖實現http://code.google.com/p/struts2-jquery/wiki/SelectTag#A_simple_Doubleselect_with_Topics,但我似乎無法成功地將json攔截器與其他攔截器相結合。JSON序列化(使用jquery插件)w/Struts2攔截器 -
在我的struts.xml:
<package name="admin" namespace="/admin" extends="struts-default,json-default">
<action name="LoadLists" method="loadLists" class="test.JSONAction">
<interceptor-ref name="json">
<param name="contentType">application/json</param>
<!--interceptor added to override this property below-->
<param name="excludeNullProperties">true</param>
</interceptor-ref>
<result name="success" type="json"/>
<interceptor-ref name="servletConfig"/>
</action>
</package>
下面是一些動作類代碼。
注意,我需要的session
可變,因此所添加的<interceptor-ref name="servletConfig"/>
線之上設置會話變量,以便它可以在下面的Java代碼使用:
public String loadLists() {
items = (List<String>) session.get("itemsList");
if (itemSelected.equals...
// Do stuff to process the list and generate the second list...
}
public void setItemSelected(String itemSelected) {
this.itemSelected = itemSelected;
}
但是當我有<interceptor-ref name="servletConfig"/>
,錯誤日誌顯示:
org.apache.struts2.json.JSONInterceptor.debug:68 - Content type must be 'application/json' or 'application/json-rpc'. Ignoring request with content type application/x-www-form-urlencoded
和可變itemSelected
從未被設置是因爲JSON序列被忽略!
如果我刪除<interceptor-ref name="servletConfig"/>
那麼我無法訪問會話!
我缺少什麼?
對不起,我的壞。我應該更徹底地測試這個。我認爲這個問題確實不是問題。通過使用默認的struts2攔截器修復。它在我的錯誤日誌中留下了很多警告,但我稍後會擔心。 – rmc 2010-12-15 14:22:58
這是一個觀察但'extends ='struts-default,json-default''最好寫成:'extends =「json-default」'因爲如果你在你的struts2-json-plugin-xxxjar下查看默認包並打開struts-plugin.xml,你會看到'package name =「json-default」extends =「struts-default」'顯然不是什麼大事,但值得了解。 – Quaternion 2010-12-16 18:31:40