2014-08-30 53 views
1

我正在使用struts 2 jquery plugin select組件。Struts 2 jquery sj:select和json結果

的操作是:

SampleAction { 

     private List<SampleVO> samples; //With setters and getters 
     private List<AnotherVO> anotherList; //With setters and getters 
     private String anString; //With setters and getters 

     @Action(value = "/loadSelect", results = { 
     @Result(name = "success", type = "json")}) 
      public String loadSomeSamples() { 
       samples = new ArrayList<SampleVO>(); 
       //Put some object in samples. 
       return SUCCESS; 
       } 
    } 

的JSP是

<sj:select list="samples" /> 

的問題是JSON插件將序列的所有屬性在行動(anotherListanString等),如低於

{ 
    "samples": { 
    "0": {"property":"a"}, 
    "1": {"property":"b"}, 
    "2": {"property":"c"} 
    }, 
    "anString": "hello", 
    "anotherList": { 
    "0": {"prop1":"a","prop2":"b"}, 
    "1": {"prop1":"c","prop2":"d"} 
    } 
} 

如果我更改了json root pa rameter到samples,那麼js:select將無法​​正常工作,因爲它在返回的json中找不到任何名爲samples的列表。返回的json是:

{ 
    "0": {"property":"a"}, 
    "1": {"property":"b"}, 
    "2": {"property":"c"} 
} 

這可以修復嗎?!有沒有什麼辦法可以配置Struts 2的JSON插件生成

{ 
    "samples": { 
     "0": {"property":"a"}, 
     "1": {"property":"b"}, 
     "2": {"property":"c"} 
     } 
    } 

或有任何爲什麼Struts 2的jQuery插件,在接受簡單的JSON陣列

回答

1

可以使用includeProperties參數JSON結果。例如

@Result(type="json", params = {"includeProperties", "samples.*" }) 

一個多個樣品

@Result(type="json", params = {"root", "samples", "wrapPrefix", "{\"samples\":", "wrapSuffix", "}"}) 
+0

第二個工作,但不是第一個。第一個返回空。然而''includeProperties「,」samples。*,samples [\\ d +]。*「}'起作用。我會將此標記爲正確的,但是您是否也可以建議採用第一種方法?! – 2014-08-30 15:07:51

+0

這是一個匹配以'samples'開始的名字的正則表達式。 – 2014-08-30 15:36:03

+0

你是對的!它似乎是一個vaid正則表達式,但我不知道爲什麼我們應該聲明'samples [\\ d +]。*'too :(我會更新如果我發現任何東西 – 2014-08-31 05:51:25