Struts 2 jquery sj:select和json结果

时间:2014-08-30 07:46:04

标签: java json jsp struts2 struts2-jquery

我正在使用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将无效,因为它无法在返回的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 个答案:

答案 0 :(得分:1)

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

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

又一个样本

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