Java Spring Controller Several ways to get request parameters
1, directly to write the parameters of the form in the controller of the corresponding method of the formal parameters, apply to get method submission, not for POST method submission. If "content-type" = "application/x-www-form-urlencoded", available post submission
URL form: http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 The submitted parameter needs to match the name of the entry parameter in the Controller method.
/**
* 1. Write the parameters of the form directly in the controller of the 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 HttpServletRequest receive, post way and get way all can.
/**
* 2, through HttpServletRequest 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, through a bean to receive, post and get way all can.
/**
* 3, through 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 @modelattribute Note to get POST request form form data
/**
* 4, use the @modelattribute note to obtain a POST request form form data
* @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, with the note @requestparam binding request parameters to the method into the parameter
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 to be
/**
* 5, Use the annotation @requestparam bind request parameter to the method enter 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, using request.getquerystring () to obtain the parameters of the spring MVC GET request, only apply to getting requests
@RequestMapping (value= "/adduser6", method=requestmethod.get) public
String ADDUSER6 (httpservletrequest request ) {
System.out.println ("username is:" +request.getquerystring ());
return "Demo/index";
}
Thank you for reading, I hope to help you, thank you for your support for this site!