springboot obtains the URL request parameters in a number of ways
- 2020-12-20 03:34:09
- OfStack
1. Write the form parameters directly in the formal parameters of the corresponding Controller method, which is applicable to get submission but not post submission.
/**
* 1. Write the form's parameters directly in 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";
}
url forms: http: / / localhost SSMDemo/demo/addUser1 & # 63; username=lixiaoxi & password=111111 The submitted parameter needs to be the same as the input name 1 in the Controller method.
2. It can be received by HttpServletRequest, post and get.
/**
* 2 And 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. It can be received by one bean, either post or get.
(1) Set up an bean corresponding to the parameters in the form
package demo.model;
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(2) This bean is used to encapsulate the received parameters
/**
* 3 And through 1 a bean To receive the
* @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. Get the parameters in the path through @PathVariable
/**
* 4 And through @PathVariable Gets the parameters in the path
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
Visit http: for example, / / localhost SSMDemo/demo/addUser4 / lixiaoxi / 111111 paths, then automatically URL template variables in {username} and {password} is bound to the through @ PathVariable annotations on the parameters of the same name, namely the username = lixiaoxi, password = 111111 refs.
5. Use the @ModelAttribute annotation to obtain the FORM form data requested by POST
The Jsp form is as follows:
<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post">
The user name : <input type="text" name="username"/><br/>
The secret code : <input type="password" name="password"/><br/>
<input type="submit" value=" submit "/>
<input type="reset" value=" reset "/>
</form>
Java Controller is as follows:
/**
* 5 , the use of @ModelAttribute Annotations to obtain POST The request of FORM The 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";
}
6. Use the annotation @RequestParam to bind the request parameter to the method
An exception occurs when the request parameter username does not exist, which can be resolved by setting the property required=false, for example: @RequestParam (value="username", required=false)
/**
* 6 , with annotations @RequestParam Bind request parameters to method parameters
* @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";
}
conclusion