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 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. Receive via HttpServletRequest, post mode and get mode
/** * 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. Receive by a bean, post mode and get mode
/** * 3, through a bean to receive * @param user * @return */@RequestMapping ("/adduser3") public String AddUser3 (Usermodel user) { Sys Tem.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 the @modelattribute annotation 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 be * * * * if "content-type" = "Application/application/json", only for Get/** * 5, with annotations @requestparam bind request parameters to the method in 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) 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