Usage of @RequestMapping annotations

Source: Internet
Author: User
Tags bind class definition
usage of @RequestMapping annotations

@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 (value= "/example") at the class definition, the map address of the Hello () method becomes: Http://localhost:8080/springMVC/helloworld

Another note is that the default property of the @RequestMapping is value, so @requestmapping (value= "/example") and @requestmapping ("/example") are 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: The @RequestMapping (value= "/helloworld", Method=requestmethod.delete) specifies that only the HelloWorld request of the DELETE method can 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") of the 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:

Package com.springmvc.RequestMapping_2;
Import Org.springframework.stereotype.Controller;

Import org.springframework.web.bind.annotation.*;  @Controller @RequestMapping ("/SPRINGMVC") public class Requestmappingtest {private static final String SUCCESS = 
    
    "Success"; /** * Annotations @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 * * Therefore, the URL directory for the Testrequestmappingurl method is:/springmvc/testrequestmappingurl * /@RequestMapping ("/testrequestmappingurl") public String Testrequestmappingurl () {System.out.println ("tes
        
        Trequestmappingurl method ... ");
    return SUCCESS;
     }/** * 1, learn: 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= "/testparamsand Hearders ", params={" View=true ", "Param"}, headers={"accept-language=zh-cn,zh;q=0.8"}) public String testparamsandhearders () {
        
        System.out.println ("Testparamsandhearders method ...");
    return SUCCESS;
     }/** * 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= "/*/testan
    T?? ")
        
        Public String Testantpath () {System.out.println ("Testantpath method ...");
    return SUCCESS; }/** * 3, the method specifies that the request must be a POST request */@RequestMapping (value= "/testmethod", Method=requestmethod.post
        
        Public String TestMethod () {System.out.println ("TestMethod method ...");
    return SUCCESS; }/** * 4, you can use the annotation @pathvariable ("id") to extract the parameters from the @requestmapping to the method's entry parameter */@RequestMapping ("/delete/{i
        
   D} ") Public String testpathvariable (@PathVariable (" id ") Integer ID) {SYSTEM.OUT.PRINTLN (" id = "+ ID);     return SUCCESS; }
}

 

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.