STRUTS2 return JSON

Source: Internet
Author: User
Tags what interface

<span style= "FONT-SIZE:18PX;" >package Com.insuper.action;import Com.insuper.service.userservice;import Com.insuper.vo.user;import Com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.modeldriven;/** * User * *  @author Seawind *  */public class Useraction extends Actionsupport implements modeldriven<user> {private String re;private User u Ser = new User (); @Overridepublic user Getmodel () {return user;} Public String Getre () {return re;} public void Setre (String re) {this.re = re;} Public String Register () throws Exception {System.out.println ("Manual User action Run ... "); Userservice.adduser (user); this.re=" Customer registration successful "; return SUCCESS;} Private UserService userservice;public void Setuserservice (UserService userservice) {this.userservice = UserService;}} </span>

This is an example of a user's manual. It is important to note that there must be a return value. The void method cannot be used. Or you can't get into the struts interceptor.

<span style= "FONT-SIZE:18PX;" ><package name= "Default" namespace= "/" extends= "Json-default" ><!--mode one, I actively assemble Service-->< to action Action name= "register" class= "com.insuper.action.UserAction" method= "register" ><result type= "JSON" >< param name= "root" >re</param></result></action></span>
we need to pay attention to extends= "Json-default"
<result type= "JSON" ><!--here Specifies the attribute to be Struts2 serialized, which must have a corresponding getter method in action-- ><!--Default will sequence the value of all getter methods that have a return value, regardless of whether the method has a corresponding property--><param name= "root" >dataMap</param><!-- Specifies whether to serialize empty properties--><param name= "Excludenullproperties" >true</param><!--here Specifies which of the properties in the RE will be serialized-->< param name= "includeproperties" >userList.*</param><!--here specifies that those attributes will be excluded from the RE, and these excluded properties are not serialized. Generally does not appear with the upper parameter configuration at the same time--><param name= "Excludeproperties" >SUCCESS</PARAM></RESULT> 
It is important to note that the JSON plugin is used to set the return result as JSON.

The principle of JSON is that the GET method in the action is serialized, and the method that was once a get is simply not specified to not serialize and will run. Assuming that the method must be named get* (for example, what interface is implemented), it is possible to annotate the method without serializing it before the method.

Annotations are: @JSON (serialize=false) in addition. JSON gaze also supports fields such as the following: Serialize: Sets whether to serialize the property deserialize: Sets whether the property is deserialized. Format: Sets the formatting used to format the output and resolve the Date form fields.

such as "Yyyy-mm-dd ' T ' HH:mm:ss". Use the gaze syntax to change the property name @json (name= "NewName") public String GetName () {return this.name;} after the attribute is serialized. Import Com.googlecode.jsonplugin.annotations.JSON needed to be introduced, @JSON (serialize=false) public User GetUser () {return this. User;} @JSON (format= "YYYY-MM-DD") public Date getstartdate () {return this.startdate;}






STRUTS2 return JSON

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.