1. Directly write the parameters of the form in the formal parameters of the corresponding method of the Controller, which is suitable for the submission of the get method, but not for the submission of the post method.
/** * 1. Directly write the parameters of the form 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=1111111 The submitted parameters need to be consistent with the parameter name in the Controller method.
2. Receive through HttpServletRequest, both post and get methods are OK.
/** * 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 are OK.
(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 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 parameters in the path through @PathVariable
/** * 4. Get 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 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 by @PathVariable, that is, after entering the parameter username=lixiaoxi and password=1111111.
5. Use the @ModelAttribute annotation to obtain the FORM form data of the POST request
The Jsp form is as follows:
<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post"> Username:<input type="text" name="username"/><br/> Password:<input type="password" name="password"/><br/> <input type="submit" value="submit"/> <input type="reset" value="reset"/> </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 the request parameters to the method to enter parameters
When the request parameter username does not exist, an exception will occur. You can solve it by setting the property required=false, for example: @RequestParam(value="username", required=false)
/** * 6. Use annotation @RequestParam to bind the request parameters to the method and enter the parameter* @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"; }Summarize
The above are the various ways to obtain URL request parameters of springboot introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!