Florent06
Florent06

Reputation: 988

Struts 2 JSON bean parsing

I tried to let Struts managing data to be parsed into JSON that way:

My bean:

public class Person implements Serializable {
    private String name;
    private String surname;

    public Person(String name, String surname) { 
        this.name = name; 
        this.surname = surname; 
    }

    /*
     * GETTERS AND SETTERS
     * ...
     */
}

My action:

public class action {

    private List<Person> people;

    private String message;

    public String execute() { 
        this.message = "HELLO";
        /*
         * Initiliaze the list
         */
        return SUCCESS;
    }

    public List<Person> getPeople() { return this.people; }
    public String getMessage() { return this.message; }
}

My struts.xml:

<struts>
    <package name="ajax-package" namespace="/ajax" extends="json-default">

        <result-types>
            <result-type name="myjson" class="org.apache.struts2.json.JSONResult">
                <param name="noCache">true</param>
            </result-type>
        </result-types>

        <action class="action" method="execute" name="action">
            <result type="myjson">
                <param name="includeProperties">
                    message, people\[\d+\]
                </param>
            </result>
        </action>
    </package>
</struts>

But, if I put an entry in my list, it is represented, in JSON, by a list with an unique empty entry:

{"message":"HELLO","people":[{}]}

It tried to use GSON to serialize my list, but struts escapes quotes.

Upvotes: 1

Views: 1401

Answers (1)

Florent06
Florent06

Reputation: 988

I found the solution in this thread:

<param name="includeProperties">
    message, people\[\d+\]\..*
</param>

This will include all properties that are accessible through get method.

Upvotes: 1

Related Questions