Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

java - Struts 2 jquery sj:select and json result

I am using struts 2 jquery plugin select component.

The action is:

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;
              }
    }

The jsp is

<sj:select list="samples" />

The problem is that the json plugin will serialize all the properties in action ( anotherList, anString etc...), as below

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

If I change the json root parameter to samples, then the js:select will not work as it can not find any list named samples in the returned json. The returned json is:

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

Can this be fixed ?! Is there any way I can configure struts 2 json plugin to generate

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

Or is there any why struts 2 jquery plugin in accept the simple json array

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use includeProperties parameter to json result. For example

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

one more sample

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...