about using the Struts2-json-plugin plugin, JSON returns the list object data as a null solution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.