Java Spring Controller Several ways to get request parameters
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. If "Content-Type"="application/x-www-form-urlencoded", you can submit it by post.
url form: http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=1111111 The submitted parameters need to be consistent with the parameter name in the Controller 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"; } 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.
/** * 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. Use the @ModelAttribute annotation to obtain the FORM form data of the POST request
/** * 4. 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"; } 5. 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) **** If "Content-Type"="application/x-www-form-urlencoded", post get can **** If "Content-Type"="application/application/json", only get /** * 5. 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"; } 6. Use request.getQueryString() to get the parameters of spring MVC get request, and only get request is applicable
@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 it can help you. Thank you for your support for this site!