Reference URL: Spring receives AJAX parameters in several ways@ModelAttribute Annotations
Using @modelattribute This method can directly map parameters into Pojo objects, I do not add @modelattribute annotations, directly receive Pojo objects, can also receive parameters
front-End Ajax requests
<Scripttype= "Text/javascript"> $(function() {$.ajax ({type:"Post", URL:"http://localhost:8080/test", data:{"name":"Zhang San", "Phone":"10086", "Password":"123456"}, Async:true, Success:function(data) {Console.log (data); } }); }) </Script>Java Receive parameters
@RequestMapping ("/test") @ResponseBody public String testUser (@ModelAttribute ("TUser") ) {TUser user) { System.out.println (User.getname ()); System.out.println (User.getpassword ()); System.out.println (User.getphone ()); return "OK"; }
Mapping results
@PathVariabl Annotations
@PathVariable is to map the specified segment point on the requested path to the specified parameter name, @PathVariable can specify multiple, and if a parameter is not placed in the URL path, request access directly. http://localhost:8080/test. can also receive the parameters
front-End Ajax requests
<Scripttype= "Text/javascript"> $(function() {$.ajax ({type:"Post", URL:"http://localhost:8080/test/10086", data:{"name":"Zhang San", "Password":"123456"}, Async:true, Success:function(data) {Console.log (data); } }); }) </Script>Java Receive parameters
@RequestMapping ("/test/{phone}") @ResponseBody public string TestUser (String name,@ pathvariable String phone,string password) { System.out.println (name); SYSTEM.OUT.PRINTLN (phone); SYSTEM.OUT.PRINTLN (password); return "OK"; }
Mapping results
get it directly with HttpServletRequestFront-end code equivalent to case one Java receive parameter
@RequestMapping ("/test") @ResponseBody public String testUser (httpservletrequest Request, HttpServletResponse response) { System.out.println (request.getparameter ("name")); System.out.println (Request.getparameter ("Phone")); System.out.println (Request.getparameter ("password")); return "OK"; }
The mapping results are exactly the same as case one@RequestParam Binding Request ParametersThe front-end request code is equivalent to case one Java receive parameter
@RequestMapping ("/test") @ResponseBody public String testUser (@RequestParam ("name") String A, @RequestParam ("Phone") string B, string password) { /** * @RequestParam () The value inside must be the same as the parameter passed by the front end . * /System.out.println (a); System.out.println (b); SYSTEM.OUT.PRINTLN (password); return "OK"; }
The mapping results are exactly the sameSummary:
1. If the parameters passed by the front end are the same as the parameter names defined by the background interface , no annotations are required, and if all the parameters are in a Pojo object, they can be mapped directly to the Pojo object , or you can use the HttpServletRequest Receive parameters
2. If you want to use the Resfull style request method, you can use the @PathVariable to annotate
3. If the parameters passed by the front end are not the same as the parameter names defined by the background interface, then the Requestparam annotations are used
Spring receives AJAX parameters in several ways