STRUTS2 integrates JSON, requires some jar packages
:
Here are the 3 methods to integrate JSON
Method One
Output JSON data in a view resource
After the action finishes processing the user request, stores the data in a location, such as request, and returns the view, and struts jumps to the view resource, where we need to take the data out of the storage location and convert it to a JSON string, output in the view.
1 /**2 * Test returns JSON data in view by Action3 */4 PublicString testbyjsp () {5User User =NewUser ();6User.setid ("1");7User.setname ("jsonactionjsp Chinese will be garbled?"));8User.setpassword ("123");9Jsonobject Jsonobject =NewJsonobject ();TenJsonobject.accumulate ("User", user); OneJsonobject.accumulate ("Success",true); A //here a data is placed in the request object, so the result configuration of struts cannot have type= "redirect" Servletactioncontext.getrequest (). SetAttribute (" Data ", jsonobject.tostring ()); - returnSUCCESS; -};
View Code
Configuration of the Struts.xml
1 < Packagename= "Default"extends= "Struts-default"namespace="/"> 2 <Actionname= "testbyjsp"3 class= "Com.scitc.test.web.UserAction" 4 Method= "testbyjsp">5 <resultname= "Success">/actionjsp.jsp</result>6 </Action>7 </ Package>
View Code
Method Two
Output JSON data in an action in a Struts2 manner
1 //objects that will be serialized as JSON strings by Struts22 PrivateMap<string, object>DataMap;3 Publicuseraction () {4DataMap =NewHashmap<string, object>();5 }6 /**7 * STRUTS2 When serializing a specified property, there must be a getter method for that property, in fact, if there is no attribute, and only the Getter method is possible8 */9 PublicMap<string, object>Getdatamap () {Ten returnDataMap; One } A /** - * Test returns JSON data by action in Struts2 default mode - */ the PublicString testbyaction () { - //the data in the Datamap will be converted to a JSON string by STRUTS2, so the data in it should be emptied first. - datamap.clear (); -User User =NewUser (); +User.setid ("2"); -User.setname ("JSONActionStruts2 Chinese will be garbled?")); +User.setpassword ("123"); A //put in a sign of whether the operation was successful atDatamap.put ("Success",true); -Datamap.put ("User", user); - returnSUCCESS; -}
View Code
Struts.xml Configuration
1 < Packagename= "JSON"extends= "Json-default"namespace="/">2 <Actionname= "Testbyaction"3 class= "Com.scitc.test.web.UserAction" 4 Method= "Testbyaction">5 <resulttype= "JSON">6 <paramname= "root">DataMap</param>7 <!--Note ie prompts you to download the requested connection resource without the following configuration -8 <paramname= "ContentType">Text/html</param>9 </result>Ten </Action> One </ Package>
View Code
Method Three
To output JSON data in action in the traditional way
This is basically the same as the traditional servlet approach, the code is as follows
1 /**2 * The traditional way to return JSON data via action3 */4 Public voidDoAction ()throwsIOException {5HttpServletResponse resp =servletactioncontext.getresponse ();6Resp.setcontenttype ("text/html");7OutputStream out =Resp.getoutputstream ();8 //the object that will be returned to the client9User User =NewUser ();TenUser.setid ("3"); OneUser.setname ("Jsonactiongeneral Chinese will be garbled?")); AUser.setpassword ("123"); -Jsonobject JSON =NewJsonobject (); -Json.accumulate ("User", user); theJson.accumulate ("Success",true); - Out.write (Json.tostring (). GetBytes ()); - out.close (); -}
View Code
Struts.xml Configuration
1 < Packagename= "Default"extends= "Struts-default"namespace="/">2 <Actionname= "Testjsonfromactionbygeneral"3 class= "Com.scitc.test.web.UserAction" 4 Method= "DoAction">5 <!--because it is a traditional request, there is no <result/> result page corresponding to it, that is, only the <action> request -6 </Action>7 </ Package>
View Code
Of these three methods, only method two <package> to inherit "Json-default"
The effects of the three methods are:
STRUTS2 Integration JSON