1

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

1 Answers1

1

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", "}"})
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The second one works but the first one not. The first one returns empty. however `"includeProperties","samples.*,samples[\\d+].*"}` works. I will mark this as correct, but can you suggest on first approach too ?! – Alireza Fattahi Aug 30 '14 at 15:07
  • It's a regex to match names starting with `samples`. – Roman C Aug 30 '14 at 15:36
  • you are correct! It seems a vaid regex but I don't know why we should declare the `samples[\\d+].*` too :( I'll update if I find anything – Alireza Fattahi Aug 31 '14 at 05:51
  • 2
    It's used for indexed properties, you shouldn't use it because it's 1) wrong regex, square brackets should be escaped. 2) you already have regex that matches all properties starting with `samples` including all indexed properties. Choose `samples\\[\\d+\\]\\..*` to match all list elements properties but not an empty list. See [this](http://stackoverflow.com/a/21350079/573032). – Roman C Aug 31 '14 at 09:18
  • Yes my reg was not correct the brackets are especial characters in regex! This link may help others http://regexr.com/39ds6 – Alireza Fattahi Aug 31 '14 at 11:09