SpringMVC @ Request, springmvc @ request
I started my blog with a new impetus during this time. Start with parameter binding.
@ RequestMapping
RequestMapping is an annotation used to process request address ing and can be used for classes or methods. Used on the class to indicate that all methods in the class that respond to requests use this address as the parent path. (This is the way in the http path, the path in the value)
The RequestMapping annotation has six attributes:
1. value and method attributes;
Value: Specifies the actual request address. The specified address can be in URI Template mode;
The uri values of values are as follows:
A) can be specified as A normal value. (value = "/test ")
B) can be specified as a type of value containing a variable (value = "/tmp/{tmp }")
C) can be specified as a class of values containing regular expressions (/web/{UserId: [a-z-]})
Method: Specifies the method type of the request, such as GET, POST, PUT, and DELETE;
(Method = RequestMethod. GET \ RequestMethod. POST)
2. consumes and produces;
Consumes: specifies the Type of Content submitted for request processing (Content-Type ),
For example, onsumes = "application/json" and "text/html"; the method only processes Requests whose Content-Type is "application/json.
Produces: specifies the type of returned content. It is returned only when the (Accept) type in the request header contains the specified type. produces = "application/json"
3. params, headers;
Params: specifies that the request must contain certain parameter values for processing. Example: (params = "myParam = start ")
Headers: specifies that the request must contain some specified header values before the method can process the request. (Headers = "www.baidu.com /")
There are two methods to obtain parameters at the SpringMVC background control layer:
1. request. getParameter ("name ");
2. directly obtain it using annotation @ RequestParam;
Description:
1. Configure through @ RequestParam: Value = "XX"
// Specify the following parameter as test. If the parameter test is not passed, an error is returned.
@ RequestParam (value = "test") String test
Error message:
HTTP Status 400-Required String parameter 'test' is not present
2. Whether the frontend parameter configured by @ RequestParam must be passed through required = false or true
// If required is set to false, the parameter is assigned null. If required is set to true
@ RequestParam (value = "test", required = false) String test
3. If the @ RequestParam annotation parameter is usedTypeIt is int and required = false at the same time. If the parameter value is not set, an error is returned. If the parameter value is not set, null is assigned to the int.
Solution:
"Consider declaring it as object wrapper for the corresponding primitive type .". We recommend that you replace the basic type with the packaging type. For example, use "Integer" instead of "int ".