一尘不染

Struts 2 jQuery sj:select和json结果

jsp

我正在使用struts 2 jquery插件选择组件。

动作是:

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参数更改为samples,则js:select不会起作用,因为它找不到samples在返回的json
中命名的任何列表。返回的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数组


阅读 234

收藏
2020-06-08

共1个答案

一尘不染

您可以使用includeProperties参数json结果。例如

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

多一个样本

@Result(type="json", params = {"root", "samples", "wrapPrefix", "{\"samples\":", "wrapSuffix", "}"})
2020-06-08