jqueryjsonjquery-uistruts2struts2-json-plugin

Serialization issue with Struts2 JSON for jQuery-ui auto-complete


I'm attempting to use Struts2 JSON plugin to serialize a JSON for a jQuery-ui autocomplete AJAX call. The format is from: jQueryUI Docs

An array of objects with label and value properties:

 [ { label: "Choice1", value: "value1" }, ... ]

I have this POJO:

public class AutoCompleteJqueryBean {

    private Long value;
    private String label;


    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public AutoCompleteJqueryBean(String label, Long value){
        this.label = label;
        this.value = value;
    }
}

Which is serializing to this JSON:

{"sponsors":[{"label":{"label":"A Duplicate"},"value":{"value":410}},{"label":{"label":"A Duplicate 2"},"value":{"value":319}},{"label":{"label":"A Duplicate 3"},"value":{"value":128}},{"label":{"label":"A Duplicate 4"},"value":{"value":191}}]}

I've also tried maps to no avail. How can I force the jQuery Autocomplete format?

The action has an array of AutoCompleteJqueryBean with public getters/setters.


Solution

  • Struts2 JSON plugin is serializing your entire action.

    If sponsors is a List<AutoCompleteJqueryBean> (or some other type of array or collection) and you want to prevent returning it in your result, you need to set it as root object in the configuration:

    <result type="json">
        <param name="root">sponsors</param>
    </result>