Several methods for Java Spring Controller to get request parameters

Source: Internet
Author: User

Technology Exchange Group: 233513714

1, directly the parameter of the form is written in the parameters of the controller corresponding method, applicable to the Get method submission, not for post mode submission. If "content-type" = "application/x-www-form-urlencoded", post submission is available

URL form: http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 The submitted parameters need to match the name of the incoming parameter in the Controller method.

/**   * 1. Write the parameter of the  form directly in the formal parameter of the controller corresponding method @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, receive via HttpServletRequest, post mode and get mode

/**   *  2. Receive @param  request  via HttpServletRequest@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 by a bean, post mode and get mode

/**@param@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 @modelattribute annotations to get the form form data for the POST request

/**    * 4. Use @modelattribute annotations to get the   form form data for 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. Binding request parameters to method @requestparam with annotations

An exception occurs when the request parameter username does not exist, and can be resolved by setting the property Required=false, for example:

@RequestParam (value= "username", required=false)If "content-type" = "application/x-www-form-urlencoded", Post get can allIf "content-type" = "Application/application/json", apply only get/*** 5, with annotations @requestparam bind request parameters to method entry *@paramusername *@paramPassword *@return   */@RequestMapping (Value= "/adduser6", method=requestmethod.get) PublicString 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 get the parameters of the spring MVC GET request and apply only the GET request

@RequestMapping (value= "/adduser6", method=requestmethod.get) public String ADDUSER6 (   HttpServletRequest request) {System.out.println ("username is:" +request.getquerystring ());  return "Demo/index";}

Several methods for Java Spring Controller to get request parameters

Related Article

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.