Several methods for obtaining springmvc request parameters

 

 

1. Write the parameters of the form directly in the formal parameters of the corresponding method of the Controller, which is suitable for the get method submission, but not for the post method submission.

copy code
    /**
     * 1. Write the parameters of the form directly in the formal parameters of the corresponding method of the 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";
    }
copy code

URL form: http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111  The parameters submitted must be the same as the parameter names in the Controller method.

2. Received through HttpServletRequest, both post and get methods are available.

copy code
    /**
     * 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";
    }
copy code

3. Receive through a bean, both post and get methods are available.
(1) Create a bean corresponding to the parameters in the form

copy code
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;
    }
    
}
copy code

(2) Use this bean to encapsulate the received parameters

copy code
    /**
     * 3. Receive through a 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";
    }
copy code

4. Get the parameters in the path through @PathVariable

copy code
    /**
     * 4. Get the parameters in the path through @PathVariable
      * @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"; }
copy code

For example, when accessing the path http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111  , the template variables {username} and {password} in the URL are automatically bound to the parameters of the same name annotated with @PathVariable, that is, the input parameters After username=lixiaoxi, password=111111.
5. Use the @ModelAttribute annotation to obtain the FORM form data
Jsp form of the POST request as follows:

copy code
<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post">
     用户名: <input type="text" name="username"/><br/>
     密  码: <input type="password" name="password"/><br/>
     <input type="submit" value="提交"/>
     <input type="reset" value="重置"/>
</form>
copy code

The Java Controller is as follows:

copy code
    /**
     * 5. Use the @ModelAttribute annotation to obtain the FORM form data of 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";
    }
copy code

6. Use the annotation @RequestParam to bind request parameters to method input parameters

When the request parameter username does not exist, an exception will occur, which can be solved by setting the attribute required=false, for example: @RequestParam(value="username", required=false)

copy code
    /**
     * 6. Use the annotation @RequestParam to bind the request parameters to the method input 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";
    }
copy code

1. Write the parameters of the form directly in the formal parameters of the corresponding method of the Controller, which is suitable for the get method submission, but not for the post method submission.

copy code
    /**
     * 1. Write the parameters of the form directly in the formal parameters of the corresponding method of the 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";
    }
copy code

URL form: http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111  The parameters submitted must be the same as the parameter names in the Controller method.

2. Received through HttpServletRequest, both post and get methods are available.

copy code
    /**
     * 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";
    }
copy code

3. Receive through a bean, both post and get methods are available.
(1) Create a bean corresponding to the parameters in the form

copy code
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;
    }
    
}
copy code

(2) Use this bean to encapsulate the received parameters

copy code
    /**
     * 3. Receive through a 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";
    }
copy code

4. Get the parameters in the path through @PathVariable

copy code
    /**
     * 4. Get the parameters in the path through @PathVariable
      * @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"; }
copy code

For example, when accessing the path http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111  , the template variables {username} and {password} in the URL are automatically bound to the parameters of the same name annotated with @PathVariable, that is, the input parameters After username=lixiaoxi, password=111111.
5. Use the @ModelAttribute annotation to obtain the FORM form data
Jsp form of the POST request as follows:

copy code
<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post">
     用户名: <input type="text" name="username"/><br/>
     密  码: <input type="password" name="password"/><br/>
     <input type="submit" value="提交"/>
     <input type="reset" value="重置"/>
</form>
copy code

The Java Controller is as follows:

copy code
    /**
     * 5. Use the @ModelAttribute annotation to obtain the FORM form data of 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";
    }
copy code

6. Use the annotation @RequestParam to bind request parameters to method input parameters

When the request parameter username does not exist, an exception will occur, which can be solved by setting the attribute required=false, for example: @RequestParam(value="username", required=false)

copy code
    /**
     * 6. Use the annotation @RequestParam to bind the request parameters to the method input 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";
    }
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324792947&siteId=291194637