How to assign json object in Struts2

Have to say struts-json-plugin have do a lot of jobs for us. Here is the configuration for the backend.

struts.xml

I’ll skip the action part. The main part is the result tag.

<result type=”json”>

<!– This is the output json object, which should be one of the property of the action. Dont forget to new this object. –>
<param name=”root”>dataMap</param>

<param name=”excludeNullProperties”>true</param>

<param name=”includeProperties”>   userList.*</param>

<param name=”excludeProperties”>
SUCCESS
</param>
</result>

 

Action part

private Map dataMap;

//getter and setter

public String execute() {

dataMap.clear();
User user = new User();
user.setId(“123″);
user.setName(“JSONActionStruts2″);
user.setPassword(“123″);
user.setSay(“Hello world !”);
dataMap.put(“user”, user);

dataMap.put(“success”, true);

return SUCCESS;
}

 

Done!

The following two tabs change content below.

silentred

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>