Detailed explanation of several methods for Java spring controller to obtain request parameters
Several methods of obtaining request parameters by Java spring controller
1. Directly write the form parameters in the formal parameters of the corresponding methods of the controller. It is applicable to get submission, not post submission. If "content type" = "application / x-www-form-urlencoded", it can be submitted by post
URL form: http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 The submitted parameter must be consistent with the input parameter name in the controller method.
2. It can be received through HttpServletRequest, both post and get.
3. It can be received through a bean, both post and get.
4. Use the @ modelattribute annotation to obtain the form data of the post request
5. Bind request parameters to method input parameters with annotation @ requestparam
An exception occurs when the request parameter username does not exist. You can solve it by setting the attribute required = false, for example:
6. Use request Getquerystring() gets the parameters of the spring MVC get request, which is only applicable to get requests
Thank you for reading, hope to help you, thank you for your support to this site!