Spring Controller can be used to obtain request parameters.
1,Directly write the form parameters in the parameters of the corresponding method of the Controller. This is applicable to get-based submission and post-based submission. If"Content-Type" = "application/x-www-form-urlencoded", available for post submission
Url format: http: // localhost: 8080/SSMDemo/demo/addUser1? Username = lixiaoxi & password = 111111 the submitted parameter must be the same as the input parameter name in the Controller method.
/*** 1. directly write the parameters in the form parameter of the corresponding method of the Controller * @ param username * @ param password * @ return */@ RequestMapping ("/addUser1 ") public String addUser1 (String username, String password) {System. out. println ("username is:" + username); System. out. println ("password is:" + password); return "demo/index ";}
2. received through HttpServletRequest. Both the post and get methods are supported.
/*** 2. Use HttpServletRequest to receive * @ param request * @ return */@ RequestMapping ("/addUser2") public String addUser2 (HttpServletRequest request) {String username = request. getParameter ("username"); String password = request. getParameter ("password"); System. out. println ("username is:" + username); System. out. println ("password is:" + password); return "demo/index ";}
3. receive data using a bean. Both the post and get methods are supported.
/*** 3. Use a bean to receive * @ param user * @ return */@ RequestMapping ("/addUser3") public String addUser3 (UserModel user) {System. out. println ("username is:" + user. getUsername (); System. out. println ("password is:" + user. getPassword (); return "demo/index ";}
4,Use the @ ModelAttribute annotation to obtain the FORM data of the POST request
/*** 4. Use the @ ModelAttribute annotation to obtain FORM data of the POST Request * @ param user * @ return */@ RequestMapping (value = "/addUser5", method = RequestMethod. POST) public String addUser5 (@ ModelAttribute ("user") UserModel user) {System. out. println ("username is:" + user. getUsername (); System. out. println ("password is:" + user. getPassword (); return "demo/index ";}
5,Bind the request parameter to the method input parameter with the annotation @ RequestParam
If the request parameter username does not exist, an exception occurs. You can set the attribute required = false, for example, @ RequestParam (value = "username", required = false)
* *** If "Content-Type" = "application/x-www-form-urlencoded" or "post get ",
* *** If"Content-Type" = "application/json", applicable only to get
/*** 5. Bind The request parameter to the method input parameter * @ param username * @ param password * @ return */@ RequestMapping (value = "/addUser6 ", method = RequestMethod. GET) public String addUser6 (@ RequestParam ("username") String username, @ RequestParam ("password") String password) {System. out. println ("username is:" + username); System. out. println ("password is:" + password); return "demo/index ";}
6. Use request. getQueryString () to obtain the parameters of the spring MVC get request. Only the get request is applicable.
@RequestMapping(value="/addUser6",method=RequestMethod.GET) public String addUser6(HttpServletRequest request) {
System.out.println("username is:"+request.getQueryString());
return "demo/index";
}