Several methods of obtaining Spring MVC request parameters reprint: Http://www.cnblogs.com/leiOOlei/p/3658147.html One, get the parameters in the path by @pathvariabl
@RequestMapping (value= "User/{id}/{name}", method=requestmethod.get) public String PrintMessage1 (@PathVariable String ID, @PathVariable string name, Modelmap model) { System.out.println (ID); SYSTEM.OUT.PRINTLN (name); Model.addattribute ("message", "111111"); Return "users"; }
For example, when accessing the User/123/lei path, execute the above method, where the parameter Id=123,name=lei
Second, @ModelAttribute get the form form data of the POST request
The JSP form is as follows
<form method= "POST" action= "hao.do" > A: <input id= "A" type= "text" name= "a"/> B: <input id= "B" type= "text" name= "B"/> <input type= "Submit" value= "Submit"/> </form>
Java Pojo is as follows
public class pojo{ private String A; private int b; }
The Java controller is as follows
@RequestMapping (method = requestmethod.post) public String processsubmit (@ModelAttribute ("Pojo") Pojo Pojo) { return "HelloWorld"; }
Third, direct use of httpservletrequest access
@RequestMapping (method = requestmethod.get) public String GET (httpservletrequest request, httpservletresponse response ) { System.out.println (Request.getparameter ("a")); return "HelloWorld"; }
Iv. binding request parameters with annotations @requestparam
Bind request parameter A to variable a with annotation @requestparam
An exception occurs when the request parameter A does not exist, which can be resolved by setting the property Required=false.
Example: @RequestParam (value= "a", Required=false)
Controller as follows
@RequestMapping (value = "/requestparam", method = requestmethod.get) public string Setupform (@RequestParam ("a") string a , Modelmap model) { System.out.println (a); return "HelloWorld";}
Spring Basic Series--several methods of obtaining spring MVC request parameters