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;/** * Registered 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 ("Registered User Action execution ... "); Userservice.adduser (user); this.re=" users registered successfully "; return SUCCESS;} Private UserService userservice;public void Setuserservice (UserService userservice) {this.userservice = UserService;}} </span>

This is a user registration as an example, it is important to note that there must be a return value, cannot use void method, otherwise cannot enter the Struts interceptor

<span style= "FONT-SIZE:18PX;" ><package name= "Default" namespace= "/" extends= "Json-default" ><!--mode one, automatically 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>
you need to pay attention to extends= "Json-default" here.
<result type= "JSON" ><!--here Specifies the attribute to be Struts2 serialized, which must have a corresponding getter method in action-- ><!--The value of all getter methods that have a return value will be serialized by default, regardless of whether the method has a corresponding attribute--><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 the attributes to be excluded from the re will not be serialized, and generally do not appear at the same time as the parameter configuration above --><param name= "Excludeproperties" >SUCCESS</PARAM></RESULT> 
It is important to note that if you use a JSON plugin to set the return result as JSON. The principle of JSON is that the GET method in the action is serialized, so the previous method of get is executed if no serialization is specified. If the method must be named get* (for example, what interface is implemented), you can declare the method without serialization before the method is annotated. Annotations are: @JSON (serialize=false) In addition, JSON annotations support several fields: 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. For example "Yyyy-mm-dd ' T ' HH:mm:ss". Use the comment syntax to change the property name @json (name= "NewName") public String GetName () {return this.name;} after serialization of this attribute. Import Com.googlecode.jsonplugin.annotations.JSON needs to be introduced, @JSON (serialize=false) public User GetUser () {return this. User;} @JSON (format= "YYYY-MM-DD") public Date getstartdate () {return this.startdate;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.