URI Template patterns__spring

Source: Internet
Author: User
URI templates can is used for convenient access to selected parts of the A URL in A@requestmapping method.

A URI Template is a uri-like string and containing one or more variable names. When yousubstitute values for these variables, the template becomes a URI. theproposed RFC for URI Templates defineshow a URI is parameterized. For example, the URI Templatehttp://www.example.com/users/{userid} contains the variable userId. Assigning thevalue Fred to the variable yields http://www.example.com/users/fred.

In Spring MVC You can use the @PathVariable annotation on a method argument to bind ITTO the value of a URI template Varia ble

@RequestMapping (value= "/owners/{ownerid}", method=requestmethod.get) public
String Findowner (@ Pathvariable String ownerID, model model) {
    owner owner = ownerservice.findowner (ownerid);
    Model.addattribute ("owner", owner);
    return "Displayowner";
}

The URI Template "/owners/{ownerid}" specifies the variable name ownerid. When Thecontroller handles this request, the value of ownerID are set to the value found in theappropriate part of the URI. For example, where a request comes in for/owners/fred,the value of ownerID is Fred.

To process the @PathVariable annotation, Spring MVC NE EDS to find the matching UriTemplate variable by name. Can specify it in the annotation:

 @RequestMapping (value= "/owners/{ownerid}", Method=requestmethod.get) public string Findowner ( @PathVariable ("ownerID")  string Theowner, Model Model) {//implementation omitted} 

Or if the URI template variable name matches the method argument name yo u can omit thatdetail. As long as your code is isn't compiled without debugging information, Spring Mvcwill match the method argument name to the U RI template variable name:

 @PathVariable  string ownerid, model model) {//Implementa tion omitted} 

A can have any number of @PathVariable annotations:

@RequestMapping (value= "/owners/{ownerid}/pets/{petid}", method=requestmethod.get) public
String Findpet (@ Pathvariable@PathVariable String Petid, model model) {
    owner owner = ownerservice.findowner (ownerid) ;
    Pet pet = Owner.getpet (Petid);
    Model.addattribute ("Pet", pet);
    return "Displaypet";
}

When a @PathVariable annotation are used on a map<string, string> argument, the Mapis populated and all URI template Variables.

A URI template can is assembled from type and path level @RequestMappingannotations. As a result the Findpet () can is invoked with a URL such as/owners/42/pets/21.

@Controller
@RequestMapping ("/owners/{ownerid}") public
class Relativepathuritemplatecontroller {

    @RequestMapping ('/pets/{petid} ') public
    void Findpet (@PathVariable String ownerid, @PathVariable String Petid, model model) {
        //implementation omitted
    }

}

A @PathVariable argument can is of any simple type such as int, long, Date, etc. Spring automatically converts to the appropriate type or throws atypemismatchexception if it fails to did so. can also register support for parsingadditional data types. "called" Method Parameters and Type conversion "and" called "Customizing Webdatabinder Initializ Ation ".

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.