Java Spring Controller several methods for obtaining request parameters

  • 2020-05-19 04:53:49
  • OfStack

Several ways to get request parameters for Java Spring Controller

1. Write the parameters of the form directly in the parameters of the corresponding method of Controller, which is suitable for get submission, but not for post submission. If "Content-Type "="application/ x-www-form-urlencoded ", post can be submitted

url forms: http: / / localhost: 8080 / SSMDemo/demo/addUser1 & # 63; The parameters submitted by username=lixiaoxi&password=111111 need to correspond to the input name 1 in the Controller method.


 /**
   * 1. Write the form's parameters directly in Controller In the corresponding method's parameter 
   * @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, either post or 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. Receive through 1 bean, either post or get.


  /**
   * 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 FORM form data requested by POST using the @ModelAttribute annotation


/**
   * 4 , 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";
  }

5. Bind the request parameter to the method with annotation @RequestParam

When the request parameter username does not exist, there will be an exception, which can be solved 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" , is only applicable get
   /**
   * 5 , with annotations @RequestParam Bind request parameters to method input 
   * @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. Get the parameters of the spring MVC get request with request.getQueryString (), only applicable to the get request


  @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 of this site!


Related articles: