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!