Mode one: self-structuring JSON
1) Import the jar package:
XStream or Jackson, recommended Jackson, high efficiency
2) Introduction of STRUTS2, writing configuration file
3) Modify the action configuration, modify the JSP page returned by result, the page only leaves ${requestscope.json}
Here is an example of the Android server I did earlier:
Action Configuration
<!--set a global forwarding address that specifically sends JSON objects-because I do all the information in the background is in JSON format, so I use the global settings <Global-results> <resultname= "Success">/web-inf/return.jsp</result> <resultname= "Input">/err.jsp</result> </Global-results>
Action class
/*** Log in * Add data to session*/ PublicString Login () {User login=userservice.login (user); Actioncontext AC=Actioncontext.getcontext (); Map<string, object> application =ac.getapplication (); if(Application.get ("User:" + user.getid ()) = =NULL) Application.put ("User:" +User.getid (), user); Actionutil.putrequestparam (Jsonutil.tojson (login));//The sentence is the focus, convert the object to JSON, and then save to the domain object return"Success"; }
public class Actionutil {
public static void Putrequestparam (String res) {
Actioncontext context = Actioncontext.getcontext (); Because the action is multiple, you can get the Actioncontext object directly outside
map<string, object> requestmap = Context.getcontextmap ();
Requestmap.put ("result", res);
}
}
return.jsp
1 <%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%>2${result}
The browser gets to the data and then calls the trim () method to remove the empty line
Method Two: Use the Struts2 Struts2-json-plugin.jar plugin to return the JSON data.
Reproduced in this section: From: http://www.cnblogs.com/liubaozhe/p/4418741.html
First, the main steps are:
1. Copy the Struts2-json-plugin.jar plugin to the project's "/web-inf/lib" folder;
2. Write the action class file;
3. Configure this action in the Struts.xml file, the action "<package.../>" must Inherit "Json-default", the action of the Result type is JSON, that is type= "json "And does not correspond to any view resources.
Second, the sample code:
Action class file:
Package Com.example.action;import Java.util.arraylist;import com.opensymphony.xwork2.actionsupport;public Class Strutsjsonaction extends Actionsupport {private int i=123; Private String str= "str"; Private int[] array={1,2,3}; Private arraylist<string> list; public int Geti () {return i; } public void SetI (int i) {this.i = i; } public String Getstr () {return str; } public void Setstr (String str) {this.str = str; } public int[] GetArray () {return array; } public void SetArray (int[] array) {This.array = array; } public arraylist<string> GetList () {return list; } public void setlist (arraylist<string> list) {this.list = list; } public String Execute () {list = new arraylist<string> (); List.add ("Red"); List.add ("green"); List.add ("yellow"); return SUCCESS; }}
Struts.xml file:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd "><struts> <package name=" json-example "namespace="/"extends=" Json-default "> <action name= "jsonexample" class= "com.example.action.StrutsJsonAction" > <result name= "Success" Type= "json"/> </action> </package></struts>
Then access "Http://localhost:8080/Struts2_JSON/JSONExample" in the browser to display the results
The JSON plug-in serializes all serializable Action properties into JSON data.
Third, configure the common JSON type of result
Whether the browser caches JSON
<result type= "JSON" > <!--Cancel Browser cache--<param name= "NoCache" >true</param></result>
Set browser response type, default to Text/html
<result type= "JSON" > <!--Setting the server response type--<param name= "ContentType" >application/json</param> </result>
Exclude a property with a value of NULL
<result type= "JSON" > <!--exclude a null Property-- <param name= "Excludenullproperties" >true</ Param></result>
Serializes the specified Action property only
<result type= "JSON" > <!--serialize only the list property within the action-- <param name= "root" >list</param> </result>
Serialization of contained attributes (comma-delimited list of regular expressions)
<result type= "JSON" > <!--Serialization List Properties-- <param name= "Includeproperties" >list.*</param ></result>
<result type= "JSON" > <!--serialize the array property, \[and \] matches the [] brackets of the arrays, \d matches the number, + represents one or more times-- <param name= " Includeproperties ">array\[\d+\]</param>
</result>
Exclude attributes that need not be serialized (comma-delimited list of regular expressions)
<result type= "JSON" > <!--exclude List Properties--<param name= "excludeproperties" > List.* </param> </result>
STRUTS2 Support for JSON