@RequestMapping Parameter Description
Value: The URL address of the request that defines the processing method. (Focus)
Method: Defines the HTTP method type for processing methods, such as GET, POST, and so on. (Focus)
Params: Defines the parameters that must be included in the requested URL. Or some parameters are not included. Know
Headers: Defines the parameters that must be included in the request headers. Or some parameters are not included. Know
the use of @RequestMapping
@RequestMapping have two ways of labeling, one at the class level and one at the method level. When the callout is on a method, value represents the URL address that accesses the method. When labeling on a class, value is the equivalent of a namespace, which means that any method under which the Controller is accessed needs to be taken with that namespace. For example:
Java code
1 @Controller2@RequestMapping ("/example")3 Public classExamplecontroller {4 5 @RequestMapping6 PublicString Execute () {7 return"Example_page";8 }9 Ten@RequestMapping ("/todo") One PublicString dosomething () { A return"Example_todo_page"; - } - the}
1:/example.action: Executes the Execute () method. The @RequestMapping annotation of the Execute () method defaults to the value, in which case the method is executed by default when the namespace is accessed. @RequestMapping annotations At the method level are required, otherwise the method cannot be accessed correctly.
2:/example/todo.action is performing the DoSomething () method. The @RequestMapping label at the class level is not required, and the URL defined on the method is an absolute address if it is not written, otherwise the URL defined on the method is relative to the Controller in which it resides.
@RequestMapping (method): Specify how pages are requested
1 @RequestMapping (value = "/register", method = requestmethod.get)2 Public String Register () {3 return "Example_register_page"; 4 }
Once the value of method is specified, the processing method only processes requests for the specified HTTP method type. Here method/register can only use GET request, cannot access using POST request
1@RequestMapping (value = "/register", method =requestmethod.get)2 PublicString Register1 () {3 return"Example_register_get_page";4 }5 6@RequestMapping (value = "/register", method =requestmethod.post)7 PublicString Register2 () {8 return"Example_register_post_page";9}
You can map the same URI for multiple methods, different HTTP method types, and Spring MVC can differentiate these methods according to the requested method type. When/example/register.action is submitted as a GET, Spring MVC calls Register1 () to process the request and, if submitted as a POST, Register2 () to process the submitted request.
If the method is not specified by default, it does not mean that it will only handle requests for GET mode by default, but it can handle requests of HTTP method types in any way. The method is specified to refine the mapping (narrowing the mapping range of the processing method), and its mapping range is the largest if method is not specified.
@RequestMapping (params)
Similar to method, the function is to refine the mapping. The processing method is called only if the URL contains a request that matches the parameters of the params value.
1@RequestMapping (value = "/find", params = "target")2 PublicString Find1 () {3 return"Example_find1_page";4 }5 6@RequestMapping (value = "/find", params = "!target")7 PublicString Find2 () {8 return"Example_find2_page";9 }Ten One@RequestMapping (value = "/search", params = "target=product") A PublicString search1 () { - return"Example_search1_page"; - } the -@RequestMapping (value = "/search", params = "target!=product") - PublicString Search2 () { - return"Example_search2_page"; +}
Find1 (): The target parameter must be in the requested URL to be able to reach this method. such as/example/find.action?target or/example/find.action?target=x, etc.
Find2 (): The requested URL must not have the target parameter in order to reach this method. such as/example/find.action or/example/find.action?q=x, etc.
Search1 (): The requested URL must have a target=product parameter in order to reach this method. such as/example/search.action?target=product, etc.
SEARCH2 (): The requested URL must not have the Target=product parameter in order to reach this method. such as/example/search.action?target=article, etc.
@RequestMapping (Headers)
The headers function is also used to refine the mapping. The processing method is called only if the requested request Headers contains parameters that match the heanders value.
1 @RequestMapping (value = "/specify", headers = "accept=text/*")2 Public String Specify () {3 return "Example_specify_page"; 4 }
The value of the Accept in the requested request Headers must match text/* (such as text/html) before the method is called.
@RequestMapping wildcard with Ant-style support
wildcard characters |
Description |
Example |
? |
Match an arbitrary character |
/a/?b can match/A/AB;/A/CB. But it doesn't match/a/acb or something. |
* |
Match any length of character |
/a/*b can match/A/CB;/A/ACB. But it doesn't match/a/cb/vb. |
** |
Matching multi-layer paths |
Can match/a/ab;/a/acb;/a/ab/abc/.../... |
Spring MVC @RequestMapping Annotations