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.
/** * 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"; }
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.
/** * 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"; }
3. Receive through a bean, both post and get methods are available.
(1) Create a 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) Use this bean to encapsulate the received parameters
/** * 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"; }
4. Get the parameters in the path through @PathVariable
/** * 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"; }
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:
<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>
The Java Controller is as follows:
/** * 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"; }
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)
/** * 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"; }
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.
/** * 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"; }
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.
/** * 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"; }
3. Receive through a bean, both post and get methods are available.
(1) Create a 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) Use this bean to encapsulate the received parameters
/** * 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"; }
4. Get the parameters in the path through @PathVariable
/** * 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"; }
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:
<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>
The Java Controller is as follows:
/** * 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"; }
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)
/** * 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"; }