Reputation: 3079
For example, with the URL
foo.com/bar/99
99 will be available directly to a method in the controller as an argument. The controller is mapped to /bar
For anyone familiar with ASP.NET MVC or Django, this would be similar to routes.MapRoute in the former and using (?P\d+) in urlpatterns in the latter.
It would be possible to process the data in the Http Request object directly to get this, but I'd like to know if Spring MVC has built-in support for this (particularly version 2.5).
Upvotes: 2
Views: 1891
Reputation: 3079
For anyone using Spring 3, I've learned this can be done using the new @PathVairable annotation
Here's the example from http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/
@RequestMapping("/hotels/{hotelId}")
public String getHotel(@PathVariable String hotelId, Model model) {
List<Hotel> hotels = hotelService.getHotels();
model.addAttribute("hotels", hotels);
return "hotels";
}
Upvotes: 12
Reputation: 100706
No, Spring does not support this out of the box. You can, however, map path to method name using @RequestMapping and appropriately configured InternalPathMethodNameResolver
:
@Controller
@RequestMapping("/bar/*")
public class MyController {
...
public String do99(HttpServletRequest request) {
...
}
}
You'll have to specify prefix
for InternalPathMethodNameResolver
as "do" for the above to work since method names can't begin with digit. If you were to use "foo.com/bar/baz" as URL and call your method baz
, no additional configuration would be necessary.
If you don't want to use method name mapping, getting '99' as parameter is as easy as grabbing it from request.getPathInfo()
in your method.
Upvotes: 0