例如,使用 URL
foo.com/bar/99
99 将可作为参数直接用于 Controller 中的方法。 Controller 映射到/bar
对于任何熟悉 ASP.NET MVC 或 Django 的人来说,这类似于前者中的 paths.MapRoute 和后者中的 urlpatterns 中使用 (?P\d+) 的情况。
可以直接处理 Http Request 对象中的数据来获取此数据,但我想知道 Spring MVC 是否对此有内置支持(特别是 2.5 版本)。
最佳答案
对于使用 Spring 3 的任何人,我了解到这可以使用新的 @PathVairable 注释来完成
这是来自 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";
}
关于java - 在 Spring MVC 中使用 URL 的一部分作为 Controller 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1534695/