Spring Controller can be used to obtain request parameters.

Source: Internet
Author: User

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"; 
  }
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.