Home  >  Article  >  Java  >  Detailed explanation of several methods for Java Spring Controller to obtain request parameters

Detailed explanation of several methods for Java Spring Controller to obtain request parameters

高洛峰
高洛峰Original
2017-01-23 10:45:122487browse

Java Spring Controller Several methods of obtaining request parameters

1. Directly write the parameters of the form in the formal parameters of the corresponding method of the Controller. It is suitable for submission by get method, but not suitable for submission by post method. If "Content-Type"="application/x-www-form-urlencoded", you can use post to submit

URL form: http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 The submitted parameters need to be consistent with the input parameter names in the Controller method.​

/**
  * 1.直接把表单的参数写在Controller相应的方法的形参中
  * @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 through HttpServletRequest, both post and get methods are available.

/**
  * 2、通过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 through a bean, either post method or get method.

/**
 * 3、通过一个bean来接收
 * @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 the @ModelAttribute annotation to obtain the FORM form data of the POST request

/**
   * 4、使用@ModelAttribute注解获取POST请求的FORM表单数据
   * @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. Use the annotation @RequestParam to bind the request parameters to Method input parameters

When the request parameter username does not exist, an exception will occur. This can be solved by setting the attribute required=false, for example:

@RequestParam(value="username", required=false)
  **** 若"Content-Type"="application/x-www-form-urlencoded",post get都可以
  **** 若"Content-Type"="application/application/json",只适用get
   /**
   * 5、用注解@RequestParam绑定请求参数到方法入参
   * @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. It is only applicable to get requests.

@RequestMapping(value="/addUser6",method=RequestMethod.GET)
public String addUser6(HttpServletRequest request) {
  System.out.println("username is:"+request.getQueryString());
  return "demo/index";
}

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed explanations of Java Spring Controller’s several methods of obtaining request parameters, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn