2. Usage of @RequestMapping annotations

Source: Internet
Author: User

@RequestMapping have the following attribute values:

1. @RequestMapping to map URLs
Annotation @RequestMapping can be used at the class definition and at the method definition.
class definition : Specifies the initial request mapping, relative to the Web application's root directory;
method definition : Further subdivide the request map, relative to the URL at the class definition. If the annotation is not used at the class definition, the URL of the method tag is relative to the root directory;

 package   com.springmvc.helloworld_1;  import   Org.springframework.stereotype.Controller;  import   org.springframework.web.bind.annotation.requestmapping;@ Controller@requestmapping (Value  = "/example" )  public  class      HelloWorld {@RequestMapping ("/helloworld ")         public   String hello () {                System.out.println ( "Hello World" );     return  "Success" ; }}

The above code specifies the mapping to "/example" at the class definition and is specified as "/helloworld" at the Hello () method. Then the URL mapping address for the Hello () method is: Http://localhost:8080/springMVC/example/helloworld

If you remove the @RequestMapping at the class definition (value= "/example"), the map address of the Hello () method becomes: http://localhost:8080/ Springmvc/helloworld

  Another note is that the default property of@RequestMapping is value, so @RequestMapping (value= "/example") and @ Requestmapping ("/example") is equivalent .



2. @RequestMapping You can specify a mapping request for request methods, request parameters, and request headers, in addition to specifying URL mappings
The value, method, params, and headers of the annotations specify the requested URL, request methods, request parameters, and request headers, respectively. There is a relationship between them, and federated use makes the request map more granular.
The 2.1 method property can specify the requested type, which specifies that there are four types of requests in http: Get,post,put,delete. The value is specified in the enumeration type Requestmethod.

For example:@RequestMapping(value="/helloworld", Method=requestmethod. Delete) specifies that only the HelloWorld request of the Delete mode will be able to execute the processing method.

2.2 Params and headers support simple expressions:
--PARAMS1: Indicates that the request must contain a request parameter named Params1
——! PARAMS1: Indicates that the request cannot contain a request parameter named Params1
--params1! = value1: Indicates that the request must contain a request parameter named PARAMS1, but its value cannot be value1
--{"params1 = value1", "param2"}: Indicates that the request must contain two request parameters named Params1 and Params2, and PARAMS1 must have a value of value1

2.3 Ant style resource address supports 3 wildcard characters:
—— ? : Match one of the characters in the file name
--*: Matches any number of characters in the file name (at least one character)
--**: Matching a multilayer path (at least one layer)

@RequestMapping support for Ant-style URLs:

--/user/create?? Match/USER/CREATEAA,/user/createbb and other URLs (?? Match any of two characters)
--/user/*/createuser match/user/aaa/createuser,/user/bbb/createuser and other URLs (* matches any character)
--/user/**/createuser matches URLs such as/user/createuser,/user/aaa/bbb/createuser, etc. (* * match any multilayer path)

Note: Its? and * must have, if empty, does not conform to

2.4 @PathVariable map placeholder for URL bindings
You can use the @PathVariable in the arguments of the controller processing method to get the placeholder parameters in the URL. The {XXX} placeholder in the URL can be bound to the entry of an action method by @PathVariable ("xxx").

@RequestMapping ("/delete/{id}") public String testpathvariable (@PathVariable ("id") Integer ID {    System.out.println ("id =" + ID);             return SUCCESS;}

  

The following is a sample class that summarizes the above knowledge points and demonstrates the application:

 Packagecom.springmvc.RequestMapping_2;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.*; @Controller @requestmapping ("/springmvc") Public classRequestmappingtest {Private Static FinalString SUCCESS = "SUCCESS"; /*** Note @RequestMapping can be used at the class definition and at the method definition * 1, at the class definition: Specify a preliminary request mapping, relative to the root of the Web application * 2, method definition: Further subdivide the request map, relative to the URL at the class definition. If the annotation is not used at the class definition, the URL of the method tag is relative to the root directory * so the URL directory for the Testrequestmappingurl method is:/springmvc/testrequestmappingurl */@RequestMapping ("/testrequestmappingurl")     PublicString Testrequestmappingurl () {System.out.println ("Testrequestmappingurl Method ..."); returnSUCCESS; }        /*** 1, Understand: You can specify the params and headers parameters. * * Params and headers values specify: *①, request parameters must contain Param, and view. Also, the value of view must be true *②, the request header must contain accept-language, and its value must be zh-cn,zh;q=0.8*/@RequestMapping (Value= "/testparamsandhearders", params={"View=true", "param"}, Headers={"accept-language=zh-cn,zh;q=0.8"})     PublicString testparamsandhearders () {System.out.println ("Testparamsandhearders Method ..."); returnSUCCESS; }        /*** 2, Ant-style placeholder.  * —— ? : Match one of the characters in the file name *--*: matches any character in the file name (at least one) *--**: matches a multilayer path (at least one layer)*/@RequestMapping (Value= "/*/testant??")     PublicString Testantpath () {System.out.println ("Testantpath Method ..."); returnSUCCESS; }        /*** 3, the method specifies that the request must be a POST request*/@RequestMapping (Value= "/testmethod", method=requestmethod.post) PublicString TestMethod () {System.out.println ("TestMethod Method ..."); returnSUCCESS; }        /*** 4, you can use the annotation @pathvariable ("id") to extract the parameters in the @requestmapping to pass into the method's arguments*/@RequestMapping ("/delete/{id}")     PublicString testpathvariable (@PathVariable ("id") (Integer ID) {SYSTEM.OUT.PRINTLN ("id =" +ID); returnSUCCESS; }}



2. Usage of @RequestMapping annotations

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.