[Spring MVC] Learning notes--@RequestMapping

Source: Internet
Author: User

@RequestMapping is used to map the URL of the request to the entire class, or to the method inside.

@Controller @requestmapping ("/test") Public classTestController {Private FinalTestservice Service; @Autowired PublicTestController (Testservice testservice) { This. Service =Testservice; } @RequestMapping (Method=requestmethod.get) PublicMap<string, test>get () {returnservice.gettests (); } @RequestMapping (Value= "/new", method=requestmethod.get) PublicTest getnewtest () {return NewTest (); }}

Include Variables in URLs
@RequestMapping (value= "/owners/{ownerid}", method=requestmethod.get) public String Findowner (@PathVariable String ownerID, model model) {    = Ownerservice.findowner (ownerid);    Model.addattribute ("owner", owner);     return "Displayowner";}

Using the variable name ownerid in the URL, when the method processes the request, the value of ownerid in the URL is passed to ownerID.

If the variable name is inconsistent, you can use the following method.

@RequestMapping (value= "/owners/{ownerid}", method=requestmethod.get) public String Findowner (@PathVariable ("ownerID") String Theowner, model model) {...}

Note 1: Variables can contain more than one.

Note 2: When used for map<string,string>, map is used to store all variables.

Matrix variables Use

Format:/cars;color=red,green,blue;year=2014 variables with ";", between the values with ",".

To use the matrix Variables, you first need to modify the XML file

<enable-matrix-variables= "true"/>

Multiple parameter examples

// get/owners/2;q=1/pets/3;q=22@RequestMapping (value= "/owners/{ownerid}/pets/{petid}", method =  Requestmethod.get)publicvoid  Findpet (        @MatrixVariable (valueint  Q1,        @MatrixVariable (valueint  Q2) {    //q1 = =    1/ /q2 = =+}

Default value

// get/pets/42@RequestMapping (value= "/pets/{petid}", method=requestmethod.get) Public void Findpet (@MatrixVariable (required=falseint  q) {     //  q = = 1   }

Get all matrix variables values

// get/owners/42;q=11;r=12/pets/21;q=22;s=23@RequestMapping (value = "/owners/{ownerid}/pets/{petid}", method = requestmethod.get)publicvoid  findpet (        @MatrixVariable Map< String,string> Matrixvars,        @MatrixVariable (Pathvar= "Petid" map<string,string>  Petmatrixvars) {    //matrixvars: ["Q": [11,22], "R": +, "s": []    // petmatrixvars: ["Q": one, "s":]}

Specify criteria

Consumes: processing occurs only if the requested Content-type value is the specified value.

@RequestMapping (..., consumes= "Application/json")

Produces: processing occurs only if the accept value in the request matches the specified value.

@RequestMapping (..., produces= "Application/json")

Params: Filtering according to the parameters in the request

@RequestMapping (..., params= "Myparam=myvalue")

Note: Expressions can be "myparam", "!myparam", "Myparam=myvalue", respectively, exist, do not exist, and are specific values.

Headers: Filtered According to the headers in the request. (Use the same params)

@RequestMapping (..., headers= "Myheader=myvalue")

Note: If it is for Content-type, it is recommended to use consumes and produces.

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.