Today's project requires the server to pass data by returning a JSON object, the data in the JSON is a list object, and the list is a bunch of custom message objects. However, when you return, you receive empty data.
Browser returns results
{"Result": []}
There must be data in the returned list, and the background test list is longer than 1.
The configuration in Struts2.xml (where result returns the result for the list specified in the action)
<!--get all kinds of--><action name= "getallvarietiesaction" class= "Com.hcj.action.database.GetAllVarietiesAction" > <result type= "JSON" name= "Success" > <!--here Specifies that those properties in the list will be serialized-- <param name= " Includeproperties "><span style=" color: #ff0000; " >result</span></param></result> </action>
In action is
Package Com.hcj.action.database;import Java.util.list;import Com.hcj.model.user;import com.hcj.service.UserService ; Import Com.opensymphony.xwork2.actioncontext;import com.opensymphony.xwork2.actionsupport;//get all public class Getallvarietiesaction extends Actionsupport {private UserService userservice;private list<string> result;public list<string> GetResult () {return result;} public void Setresult (list<string> result) {This.result = result;} Public UserService Getuserservice () {return userservice;} public void Setuserservice (UserService userservice) {this.userservice = UserService;} @Overridepublic String Execute () throws Exception {result = Userservice.getallvarieties (); SYSTEM.OUT.PRINTLN ("Query result is" + result.get (1)); return SUCCESS;}}
After querying the data, as long as the strut2.xml to make the following changes to get the correct results
<!--get all kinds--<action name= "getallvarietiesaction" class= "Com.hcj.action.database.GetAllVarietiesAction" > <result type= "JSON" name= "Success" > <span style= "White-space:pre" ></span> <!-- This specifies that those properties in the list will be serialized-- <span style= "White-space:pre" ></span> <param name= "Includeproperties" ><span style= "color: #ff0000;" >result.*</span></param><span style= "White-space:pre" ></span></result> </ Action>
The final browser returns the result as
{"Result": ["Puer_tea", "Green_tea"]}
about using the Struts2-json-plugin plugin, JSON returns the list object data as a null solution