Several methods for Spring Controller to get request parameters

Source: Internet
Author: User

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 parameters of the form directly in the 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, through the HttpServletRequest receive, the post way and get way can.
  /**     * 2, receive through HttpServletRequest      * @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, through a bean to receive, post and get the method can be.
/** * 3, through a bean to receive * @param user * @return */@RequestMapping ("/adduser3") public String AD        DUser3 (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 get the form form data for the POST request
/** * 4, use the @modelattribute annotation to get the form form data of the POST request * @param user * @return */@RequestMapping (value= " /adduser5 ", method=requestmethod.post) public String AddUser5 (@ModelAttribute (" user ") Usermodel user) {System.ou        T.println ("username is:" +user.getusername ());        SYSTEM.OUT.PRINTLN ("Password is:" +user.getpassword ());    return "Demo/index"; 
5, bind the request parameter to the method with the annotation @requestparam
when the request parameter username does not exist, an exception occurs 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
* * * * if
"content-type" = "Application/application/json", only for Get
/**     * 5, with annotations @requestparam binding request parameters to the method into the 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 get the parameters of the spring MVC GET request and apply only the GET request
@RequestMapping (value= "/adduser6", Method=requestmethod.get)    

}

Several methods for Spring Controller to get request parameters

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.