https://www.cnblogs.com/daimajun/p/7152970.html(copy)
To provide a mouth @RequestMapping ( "url"), url where the write request is part of the path, the general method of acting on the Controller, as a request address mapping.
Code:
@RequestMapping (value = "/ test" ) // class-level mapping, can not generally be used to reduce the amount of writing public class myController {// method level mapping, you must have, then the access address of this method is / test / aaa, to request the page is test.jsp [Of course, there needs to be configured in the .jsp profile] @RequestMapping (value = "/ AAA ") public String getMyName () { return" Test "; } }
So @ResponseBody it?
@ResponseBody is acting on the process, @ ResponseBody which returns the result of the method of directly writing HTTP response body is generally used in the asynchronous data acquired AJAX] {i.e., @RequestMapping after use, the return value is usually resolved hops transfer path, but returns a result after adding @ResponseBody not be interpreted as the jump path, but directly written in the HTTP response body. Such as asynchronous get json data, after adding @ResponseBody, it will return json data directly. @RequestBody the insertion process the HTTP request body, using a suitable HttpMessageConverter write request an object body.
for example:
Reception asynchronous request:'loginAction' function () { // get the user account and password input var name = $ ( '# COUNT') Val ();. . var password = $ ( '# password') Val (); $ .ajax ({ URL : 'Account / login.do', type: 'POST', the parameter attribute name // data objects to the server controller and the same name Login (name, password) Data: { 'name': name, 'password ': password }, dataType:' JSON ', success: function (Result) { IF (result.state == 0) { // login is successful, and set a cookie jump edit.html AddCookie (' the userId ', result.data .id); } else { AddCookie ( 'Nick', result.data.nick); = the location.href 'edit.html'; // Login failed var msg = result.message; $ ( '# SIG_IN') the Next () HTML (msg);.. .. $ ( '# SIG_IN') the Next () CSS ( "Color", "Red") ; } }, error: function (E) { Alert ( "system exceptions"); } }); $ ( '# password') Val ( "");. }Background Controller class corresponding methods:
@RequestMapping("/login.do") @ResponseBody public Object login(String name, String password, HttpSession session) { user = userService.checkLogin(name, password); session.setAttribute("user", user); return new JsonResult(user); }
@RequestBody it?
@RequestBody is acting on the parameter list, for transmitting data over the front fixed format [xml format or the like json encapsulated as JavaBean] corresponding to the object, when used to package an object is the default configuration HttpMessageConverter parses then packaged into a parameter.
For example, the above code can be changed to log in the background:
@RequestMapping("/login.do") @ResponseBody public Object login(@RequestBody User loginUuser, HttpSession session) { user = userService.checkLogin(loginUser); session.setAttribute("user", user); return new JsonResult(user); }