我有一个模拟我的请求的类,比如
class Venue {
private String city;
private String place;
// Respective getters and setters.
}
我想支持一个 RESTful URL 来获取有关 field 的信息。所以我有这样的 Controller 方法。
@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(@PathVariable("city") String city, @PathVariable("place") String place, Model model) {
// code
}
有没有办法,我可以在 spring 中将我的路径变量绑定(bind)到模型对象(在本例中为 Venue),而不是获取每个单独的参数?
最佳答案
Spring MVC
提供了将请求参数和路径变量绑定(bind)到 JavaBean 的能力,在您的例子中是 Venue
。
例如:
@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(Venue venue, Model model) {
// venue object will be automatically populated with city and place
}
请注意,您的 JavaBean 必须具有 city
和 place
属性才能工作。
更多信息,您可以查看withParamGroup() example from spring-projects/spring-mvc-showcase
关于java - 在 spring 中将 Path 变量绑定(bind)到自定义模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149425/