1. Binding request parameters with annotations @requestparam
Bind request parameter A to variable a with annotation @requestparam, when request parameter A does not exist an exception occurs, can be resolved by setting properties Required=false, for example: @RequestParam (value= "a", required= False
JS and Controller are as follows: JS when the post data is a JSON object:
correspond to the wording in the controller:
2. @ModelAttribute get form form data for a POST request
The JSP page is as follows:
Where the OnSubmit property is used to execute a piece of JavaScript when the form is submitted.
The corresponding controller is as follows: The name attribute value in the form is the attribute in the user of the controller in the Pojo, so that the background automatically receives the data from the form and encapsulates it into user
3. Get the parameters in the path by @pathvariabl
For example:
1@RequestMapping (value= "User/{id}/{name}", method=requestmethod.get)2 3 Publicstring PrintMessage1 (@PathVariable string ID,@PathVariable string name, Modelmap model) {4 5 6 7 System.out.println (ID);8 9 System.out.println (name);Ten OneModel.addattribute ("message", "111111"); A - return"Users"; - the}
When accessing the User/123/lei path, execute the above method, where the parameter Id=123,name=lei
4. The most primitive method: Get it directly with HttpServletRequest
1 @RequestMapping (method =2public3 System.out.println ( Request.getparameter ("A"4 return "HelloWorld"5 }
Reference: https://www.cnblogs.com/blog411032/p/5909512.html
Springmvc several ways to receive request parameters