This is what I personally see on the Internet, and I am also very careful about how to use json in struts2. In conventional WEB applications, the server returns JSON data to the client in two ways: one is to output the JSON string in the Servlet, and the other is to output the JSON string on the JSP page. As mentioned above, the service... SyntaxHighlighter. all ()
This is what I personally see on the Internet, and I am also very careful about how to use json in struts2.
In conventional WEB applications, the server returns JSON data to the client in two ways: one is to output the JSON string in the Servlet, and the other is to output the JSON string on the JSP page. As mentioned above, when a server returns JSON data like a client, it actually returns a string that complies with the JSON syntax specification. Therefore, the two methods have one thing in common, the data to be returned is packaged as a string that complies with the JSON syntax and displayed on the page.
Next, let's summarize the usage in the previous article.
Use Servlet to return JSON data to the client:
Package cn. ysh. studio. struts2.json. demo. servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import net. sf. json. JSONObject; import cn. ysh. studio. struts2.json. demo. bean. user; public class JSON extends HttpServlet {/*****/private static final long serialVersionUID = 1L;/*** The doGet method of the servlet.
** This method is called when a form has its tag value method equals to get. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doGet (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html"); PrintWriter out = response. getWriter (); // the object to be returned to the client: User user = new User (); user. setId ("123"); user. setName ("JSONServlet"); user. setPassword ("JSON"); user. setSay ("Hello, I am a servlet! "); JSONObject json = new JSONObject (); json. accumulate ("success", true); json. accumulate ("user", user); out. println (json. toString (); // because JSON data is transmitted in the form of a common string during transmission, therefore, we can also manually splice strings that conform to the JSON syntax and output them to the client // the following two sentences serve the same purpose as the 38-46 lines of code, A User object and a success field // String jsonString = "{\" user \ ": {\" id \ ": \" 123 \", \ "name \": \ "JSONServlet \", \ "say \": \ "Hello, I am a servlet! \ ", \" Password \ ": \" JSON \ "}, \" success \ ": true}"; // out. println (jsonString); out. flush (); out. close ();}/*** The doPost method of the servlet.
** This method is called when a form has its tag value method equals to post. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Return to Struts. In the Struts MVC Model, Action replaces Servlet to assume the role of Model. Therefore, for Struts, JSON data is returned to the client, just like traditional WEB applications, there are two ways to output JSON data in Action and JSON data in view resources. Further subdivided, The JSON data output in the Action can be divided into two methods: one is to use the traditional method to output the packaged JSON data, second, the JSON Data encapsulation function provided by Struts comes from dynamic packaging and JSON data is returned.
Output JSON data in view resources
After processing the user request, the Action stores the data in a certain position, such as the request, and returns the view. Then, Struts redirects to the view resource. In this view, what we need to do is to extract the data from the storage location, convert it to a JSON string, and output it in the view. This is similar to the practice of outputting JSON data on JSP pages in traditional WEB applications:
Public String testByJSP () {User user = new User (); user. setId ("123"); user. setName ("Struts2"); user. setPassword ("123"); user. setSay ("Hello world! "); JSONObject jsonObject = new JSONObject (); jsonObject. accumulate ("user", user); // here, a data is put in the request object, so type = "redirect" ServletActionContext cannot be included in the result configuration of struts. getRequest (). setAttribute ("data", jsonObject. toString (); return SUCCESS ;};