[英]Get the variable in the path of a URI
In Spring MVC I have a controller that listens to all requests coming to /my/app/path/controller/*
. 在Spring MVC中,我有一个控制器,可以监听所有进入
/my/app/path/controller/*
请求。
Let's say a request comes to /my/app/path/controller/blah/blah/blah/1/2/3
. 假设请求来到
/my/app/path/controller/blah/blah/blah/1/2/3
。
How do I get the /blah/blah/blah/1/2/3
part, ie the part that matches the *
in the handler mapping definition. 如何获取
/blah/blah/blah/1/2/3
部分,即与处理程序映射定义中的*
匹配的部分。
In other words, I am looking for something similar that pathInfo
does for servlets but for controllers. 换句话说,我正在寻找类似于
pathInfo
为servlet做的事情,但是对于控制器。
In Spring 3 you can use the @ PathVariable annotation to grab parts of the URL. 在Spring 3中,您可以使用@ PathVariable注释来获取URL的部分内容。
Here's a quick example from http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/ 以下是http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/的简单示例
@RequestMapping(value="/hotels/{hotel}/bookings/{booking}", method=RequestMethod.GET)
public String getBooking(@PathVariable("hotel") long hotelId, @PathVariable("booking") long bookingId, Model model) {
Hotel hotel = hotelService.getHotel(hotelId);
Booking booking = hotel.getBooking(bookingId);
model.addAttribute("booking", booking);
return "booking";
}
In Spring 2.5 you can override any method that takes an instance of HttpServletRequest as an argument. 在Spring 2.5中,您可以覆盖任何将HttpServletRequest实例作为参数的方法。
org.springframework.web.servlet.mvc.AbstractController.handleRequest org.springframework.web.servlet.mvc.AbstractController.handleRequest
In Spring 3 you can add a HttpServletRequest argument to your controller method and spring will automatically bind the request to it. 在Spring 3中,您可以向控制器方法添加HttpServletRequest参数,spring将自动将请求绑定到它。 eg
例如
@RequestMapping(method = RequestMethod.GET)
public ModelMap doSomething( HttpServletRequest request) { ... }
In either case, this object is the same request object you work with in a servlet, including the getPathInfo method. 在任何一种情况下,此对象都与您在servlet中使用的请求对象相同,包括getPathInfo方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.