Spring MVC @RequestMapping Explanatory notes (2)

Source: Internet
Author: User

@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")3PublicClassExamplecontroller {45 @RequestMapping  6 public< Span style= "COLOR: #000000" > String execute () { 7 return " Example_page "; 8 } 9 10 @RequestMapping ("/todo" 11 public String dosomething ( {12 return "Example_todo_page" ; 13 }14 15}               

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)public String Register () {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 =2 public  String Register1 () {3  Return "Example_register_get_page" ; 4 }5 6 @RequestMapping ( Value = "/register", Method = Requestmethod.post) 7 public String 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")2PublicString Find1 () {3Return "Example_find1_page";4}56 @RequestMapping (value = "/find", params = "!target")7PublicString Find2 () {8Return "Example_find2_page";9}1011 @RequestMapping (value = "/search", params = "target=product" ) 12 public String search1 () {13 return" Example_ Search1_page "; }15 16 @RequestMapping ( Value = "/search", params = "target!=product" ) 17 public String Search2 () {18 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/*")public String Specify () {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/.../...

Category: Springmvc

Spring MVC @RequestMapping Explanatory notes (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.