URI Template Patterns

URI templates can be used for convenient access to selected parts of a URL in a@RequestMapping method.

A URI Template is a URI-like string, 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 variable:

@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 is set to the value found in theappropriate part of the URI. For example, when a request comes in for /owners/fred,the value of ownerId is fred.

[Tip]

To process the @PathVariable annotation, Spring MVC needs to find the matching URItemplate variable by name. You 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 you can omit thatdetail. As long as your code is not compiled without debugging information, Spring MVCwill match the method argument name to the URI template variable name:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
    // implementation omitted
}

A method can have any number of @PathVariable annotations:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @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 is used on a Map<String, String> argument, the mapis populated with all URI template variables.

A URI template can be assembled from type and path level @RequestMappingannotations. As a result the findPet() method can be 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 be of any simple type such as int, long, Date, etc.Spring automatically converts to the appropriate type or throws aTypeMismatchException if it fails to do so. You can also register support for parsingadditional data types. See the section called “Method Parameters And Type Conversion” and the section called “Customizing WebDataBinder initialization”.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值