在Spring中将路径变量绑定到自定义模型对象 [英] Bind Path variables to a custom model object in spring
问题描述
我有一个模拟我的请求的课程,例如
I have a class that models my request, something like
class Venue {
private String city;
private string place;
// Respective getters and setters.
}
我想支持一个RESTful URL来获取有关场地的信息。所以我有这样的控制器方法。
And I want to support a RESTful URL to get information about a venue. So I have controller method like this.
@RequestMapping(value = "/venue/{city}/{place}", method = "GET")
public String getVenueDetails(@PathVariable("city") String city, @PathVariable("place") String place, Model model) {
// code
}
有没有办法,我可以说在春天将我的路径变量绑定到模型对象(在这个案例Venue)而不是获取每个单独的参数?
Is there a way, I can say in spring to bind my path variables to the model object (in this case Venue) instead of getting every individual parameter?
推荐答案
Spring MVC
提供将请求参数和路径变量绑定到JavaBean的功能,在您的情况下是 Venue
。
例如:
Spring MVC
offers the ability to bind request params and path variables into a JavaBean, which in your case is Venue
.
For example:
@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
它的工作属性。
Note that your JavaBean has to have city
and place
properties for it to work.
有关更多信息,您可以查看 withParamGroup()示例来自spring-projects / spring-mvc-showcase
For more information, you can take a look at the withParamGroup() example from spring-projects/spring-mvc-showcase
这篇关于在Spring中将路径变量绑定到自定义模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!