1. Obtain through ServletAPI
WillHttpServletRequest
As a formal parameter of the controller method, at this timeHttpServletRequest
The parameter of type represents the object that encapsulates the request message of the current request.
@RequestMapping("/testParam") public String testParam(HttpServletRequest request){ String username = ("username"); String password = ("password"); ("username:"+username+",password:"+password); return "success"; }
2. Obtain the request parameters through the formal parameters of the controller method
In the formal parameter position of the controller method, set the formal parameter with the same name as the request parameter. When the browser sends a request and matches the request map,DispatcherServlet
The request parameter will be assigned to the corresponding formal parameters
<a th:href="@{/testParam(username='admin',password=123456)}" rel="external nofollow" > Test to get request parameters-->/testParam</a><br>
@RequestMapping("/testParam") public String testParam(String username, String password){ ("username:"+username+",password:"+password); return "success"; }
Note:
If there are multiple request parameters of the same name in the request transmitted request parameters, you can set a string array or a string type formal parameter in the formal parameters of the controller method to receive this request parameter.
If you use a formal parameter of the string array type, the array of this parameter contains each data.
If a formal parameter of string type is used, the value of this parameter is the result of using commas splicing in the middle of each data.
3、@RequestParam
@RequestParam
It is to create a mapping relationship between the formal parameters of the request parameter and the controller method.@RequestParam
There are three attributes in the annotation:
value: The parameter name of the request parameter specified as the formal parameter assignment
required: Set whether this request parameter must be transmitted, the default value is true
If set to true, the current request must transmit the request parameter specified by the value. If the request parameter is not transmitted and the defaultValue property is not set, the page will report an error.400:Required String parameter 'xxx' is not present;
If set to false, the current request does not have to transmit the request parameters specified by the value. If there is no transmission, the value of the identified formal parameters of the annotation is null.
defaultValue: Regardless of the required attribute value is true or false, when the request parameter specified by the value is not transmitted or the transmitted value is "", the default value is assigned as the formal parameter
4、@RequestHeader
@RequestHeader creates a mapping relationship between the request header information and the formal parameters of the controller method.
The @RequestHeader annotation has three properties: value, required, and defaultValue, and the usage is the same as @RequestParam
5、@CookieValue
@CookieValue is to create a mapping relationship between cookie data and controller method formal parameters
The @CookieValue annotation has three attributes: value, required, and defaultValue, and the usage is the same as @RequestParam
6. Obtain request parameters through POJO
You can set a formal parameter of entity class type at the formal parameter position of the controller method. At this time, if the parameter name of the request parameter transmitted by the browser is consistent with the attribute name in the entity class, the request parameter will assign a value to this attribute.
<form th:action="@{/testpojo}" method="post"> username:<input type="text" name="username"> password:<input type="password" name="password"> gender:<input type="radio" name="sex" value="male">male<input type="radio" name="sex" value="female">female age:<input type="text" name="age"> Mail:<input type="text" name="email"> <input type="submit"> </form>
@RequestMapping("/testpojo") public String testPOJO(User user){ (user); return "success"; } //Final result-->User{id=null, username='Zhang San', password='123', age=23, sex='male', email='123@'}
7. Solve the problem of garbled code in obtaining request parameters
Solve the problem of garbled code in obtaining request parameters, you can useSpringMVC
The encoding filter provided by CharacterEncodingFilter, but must be registered in
<!--ConfigurationspringMVCEncoding filter--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class></filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Note:
The filters that process encoding in SpringMVC must be configured before other filters, otherwise they will be invalid.
This is the article about SpringMVC obtaining request parameters. For more relevant SpringMVC obtaining request parameters, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!