jsp part: the page is success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>success</title> </head> <body> <div> <div> <h1>用户信息</h1> </div> <div> 姓名: ${user.name} </div> <div> 年龄: ${requestScope.user.age} </div> </div> </body> </html>
1. Using String
// returns the String @RequestMapping (path = "/ StringTest" ) public String StringTest (the Model Model) { // query from the database to a value to be displayed the User User = new new the User (); user.setName ( "John Doe" ) ; user.setAge ( 18 is ); model.addAttribute ( "User" , User); return "Success" ; }
2. void
2.1. Use this request with the value transfer request field,
2.2. With a response redirect,
2.3. Response.getWriter () output stream to write directly to the page content
// void if not any jump operation, a jump to / Response / voidTest @RequestMapping (path = "/ voidTest" ) public void voidTest (the HttpServletRequest Request, Response the HttpServletResponse, String type) { // get the request api // query from the database to a value to be displayed the User User = new new the User (); user.setName ( "John Doe" ); user.setAge ( 18 is ); request.setAttribute ( "User" , User); the System.out .println ( "type =" + type); // this is a jump manually, not automatically spring to help process, it is necessary to write the entire page of the particular path, and the Model class is the spring, the value used here also pass Model die, only use Request the try{ // use request jump IF (type.equals ( "request" )) { request.getRequestDispatcher ( "/WEB-INF/pages/success.jsp" ) .forward (request, response); } // use heavy response orientation, and then jump to this method of transmitting a request to jump IF (type.equals ( "Response" )) { Response.sendRedirect (request.getContextPath () + "/ Response / StringTest"); // jump to other The method can be used across projects, to write the whole path. System.out.println (request.getContextPath () + "/ Response / StringTest" ); } // use the direct print output stream IF (type.equals ( "Writer" response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.getWriter().println("<html>\n" + "<head>\n" + " <title>success</title>\n" + "</head>\n" + "<body>\n" + "<div>\n" + " <div>\n" + " <h1>用户信息</h1>\n" + " </div>\n" + " <div>\n" + " 姓名: " + user.getName() + "\n" + " </div>\n" + " <div>\n" + " 年龄: " + user.getAge() + "\n" + " </div>\n" + "</div>\n" + "</body>\n" + "</html>"); } } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
3. Use responseBody pass value json format.
3.1 Configuration Environment
3.1.1 jquery:
Import jquery package (pasted directly into a folder on the line)
springMVC configuration file, usually referred to intercept all requests and handle spring, so js packets will be intercepted, it is necessary to modify springmvc.xml
Code:
<! - Configure front-end controller do not intercept a static resource -> < MVC: Resources LOCATION = "/ JS /" Mapping = "/ JS / **" /> < MVC: Resources LOCATION = "/ CSS /" Mapping = "/ CSS / **" /> < MVC: Resources LOCATION = "/ Images /" Mapping = "/ Images / **" />
3.1.2 json
Add spring-dependent process in json dependencies in the pom.xml
<!-- 转化json时使用的jar包--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency>
3.2 Code
jsp part
< Div > <-! Test asynchronous request -> < INPUT type = "Button" value = "test ajax request and response json json" ID = "testJson" /> </ div > < Script type = "text / JavaScript " the src =" / JS / jquery.min.js " > </ Script > < Script type =" text / JavaScript " > // test asynchronous request code $ ( function () { $ ( " #testJson " ).click(function(){ alert(" Transmission request " ); $ .ajax ({ type: " POST " , // request type URL: " $ {} pageContext.request.contextPath / Response / responseJsonTest " , // request address contentType: " file application / JSON; charset . 8-UTF = " , // prevent distortion Chinese Data: ' {" name ":" Joe Smith "," Age ":} 20 is ' , // the Data which is key to json format dataType: " JSON " , // return the data type Success: function (Data) { // This method is performed after the successful request, data is received object Alert ( " Name: " + data.name + " Age: " + data.age); // pop displays the value returned, the returned object can take the properties } }); }); }) </ Script >
java part of the controller:
// test response data json @RequestMapping ( "/ responseJsonTest" ) @ResponseBody // the returned object into ajax, json format transmitted reception data becomes public the User responseJsonTest (@RequestBody the User User) { // get the request body ( json format), to turn into json user object, the User name and age has two attributes System.out.println ( "asynchronous request:" + user); user.setName ( "John Doe" ); user.setAge ( 31 is ); System.out.println ( "return request:" + User); return User; }