The JSON data format is very much used in today's web development, and it's basically now using JSON to pass data back and forth. So, in struts and Servlets, do you return the JSON format? JSON data in the process, in fact, is to pass a normal JSON syntax format of the string, so-called "JSON object" refers to the JSON string parsing and packaging results , So what we're going to do is construct a string that satisfies the JSON format, and that's the principle. Remember, here's how I've coded to demonstrate several methods.
Requirements: We enter the username + password from the front end, back end in JSON format return username + password, front-end alert user name + password
First, construct a string
JS Code:
function login () {user_name = $ ("#user_name"). Val (); user_pwd = $ ("#user_pwd"). Val (); $.post ("User!tofun1", {user_name: User_name,user_pwd:user_pwd},function (data) {alert (data.user_name+ "" "+data.user_pwd);}," JSON ");}
Description, enter the username password in the text box, then click the button to execute the login () method, HTML code I won't post it.
USER!TOFUN1, this is the implementation of the user action in the Tofun1 this method, in the struts configuration is good, this action can not configure the result, because we do not need to return the value, of course, you configure it does not matter
Back-end code (struts and servlet generic):
Constructs a JSON-formatted string public void Tofun1 () {HttpServletRequest request = Servletactioncontext.getrequest (); HttpServletResponse response = Servletactioncontext.getresponse (); PrintWriter pw = null;try {pw = Response.getwriter ();} catch (IOException e) {e.printstacktrace ();} String user_name = Request.getparameter ("user_name"); String user_pwd = Request.getparameter ("User_pwd"); String json_str = "{\" user_name\ ": \" "+user_name+" \ ", \" user_pwd\ ": \" "+user_pwd+" \ "}";; /write the format must be correct, otherwise the front end can not parse System.out.println (JSON_STR);p w.write (json_str);p W.flush ();p w.close ();}
This method does not return a string type, and struts typically returns a string value corresponding to the view, but here we do not need, I do not return, this way is too troublesome, because we have to construct that string, the eyes are looking at the flowers, rest assured that We have some other packages that can be used to convert
Second, the rack package automatically converted to a JSON-formatted string (can be in list and map format)
function login () {user_name = $ ("#user_name"). Val (); user_pwd = $ ("#user_pwd"). Val (); $.post ("User!tofun2", {user_name: User_name,user_pwd:user_pwd},function (data) {var stu=data.map;alert (stu.user_name+ "" +stu.user_pwd);}, "JSON");}
Note that the JSON inside the $.post () is added, otherwise the returned data may not be parsed.
Back-end code (struts, servlet generic, remember to add the necessary packages):
Automatic conversion of public void tofun2 () {HttpServletRequest request = Servletactioncontext.getrequest () in a rack-and-parcel manner; HttpServletResponse response = Servletactioncontext.getresponse (); PrintWriter pw = null;try {pw = Response.getwriter ();} catch (IOException e) {e.printstacktrace ();} String user_name = Request.getparameter ("user_name"); String user_pwd = Request.getparameter ("User_pwd"); Map.put ("user_name", user_name); Map.put ("User_pwd", user_pwd); Jsonobject json = new Jsonobject (); Json.accumulate ("Map", map); System.out.println (Json.tostring ());p W.write (json.tostring ()); System.out.println (Json.tostring ());p W.flush ();p w.close ();
And the same as above, this I was using struts2 frame, but tofun2 () I did not want the string type to return the value that can be
Three, using Struts configuration mode
The JS code is the same as above
Back-end Code
Returns jsonpublic String tofun3 () {HttpServletRequest request = Servletactioncontext.getrequest () via action; String user_name = Request.getparameter ("user_name"); String user_pwd = Request.getparameter ("User_pwd"); Map.put ("user_name", user_name); Map.put ("User_pwd", user_pwd);// Request.setattribute ("map", map); return "Login";}
Note that to write the Get method of the Map property, struts returns all properties that have a Get method by default in JSON format.
Configuration file to be aware, I want to create a new package, inherit Json-default, configured as follows:
<package name= "JSON" extends= "Json-default" namespace= "/test" ><action name= "user" class= " Com.sunny.action.Login "><result name=" Login "type=" JSON "> </result></action></package>
The JSON format returned is as follows:
Add: Because struts will return all of the Get methods in JSON, so we can filter some properties, such as the above example, I just want to return the map of this property JSON format data, the following configuration
<package name= "JSON" extends= "Json-default" namespace= "/test" ><action name= "user" class= " Com.sunny.action.Login "><result name=" Login "type=" JSON "><param name=" root ">map</param> </result></action></package>
The JSON format returned is as follows
Note that the above return is different, there is a layer of map, there is no, so the front-end call to pay attention to.
Give some filtering settings for the JSON return result;
<result type= "JSON" > <!--Here Specify the property that will be Struts2 serialized, which must have a corresponding getter method in action-- <!-- By default, all getter methods with return values will be serialized, regardless of whether the method has corresponding properties-- <param name= "root" >dataMap</param> <!-- Specifies whether to serialize empty properties- <!-- <param name= "excludenullproperties" >true</param> <!--here specify which of the properties in Datamap will be serialized-- <!-- <param name= "includeproperties" > userlist.* </param> - <!--here Specifies that the attributes to be excluded from Datamap will not be serialized, and half of the parameters configured on the top are not at the same time - <!-- <param name= "Excludeproperties" >
SUCCESS </param>
</result>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Return JSON data format in Struts2, servlet