SpringWebMVC,公共干净url参数
我试图在我的MVC应用程序中构建一个干净的URL映射,我发现了许多常见的URL,如: /SITE/{city}-{language}/user/{userId}SpringWebMVC,公共干净url参数,spring,spring-mvc,Spring,Spring Mvc,我试图在我的MVC应用程序中构建一个干净的URL映射,我发现了许多常见的URL,如: /SITE/{city}-{language}/user/{userId} @RequestMapping(value = "/{city}-{language}/user/{userId}", method = RequestMethod.GET) public String user(@PathVariable String city, @Path
@RequestMapping(value = "/{city}-{language}/user/{userId}",
method = RequestMethod.GET)
public String user(@PathVariable String city,
@PathVariable String language,
@PathVariable String userId,
Model model) {}
/站点/{city}-{language}/comment/{userId}-{commentId}
@RequestMapping(value = "/{city}-{language}/comment/{userId}-{commentId}",
method = RequestMethod.GET)
public String comment(@PathVariable String city,
@PathVariable String language,
@PathVariable String userId,
@PathVariable String commentId,
Model model) {}
是否有一种方法可以自动将城市和语言绑定到模型,而不是@PathVariable of Filters,我认为这是可行的,因为它将减少@RequestMapping函数参数的计数。您只需要为每种类型实现接口: 比如说
public class StringToCityConverter<String, City> {
...
public City convert (String cityName) {
return this.cityDao.loadByCityName(cityName);
}
}
而本注册纪录册则由注册主任保存
<!-- Installs application converters and formatters -->
<bean id="applicationConversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<bean
class="MyRegistrar" autowire="byType" />
</set>
</property>
</bean>
我做了一个工作来满足我的需要,我创建了一个抽象基类来映射公共参数
public abstract class AbstractController {
@ModelAttribute("currentCity")
public CityEntity getCurrentCity(@PathVariable CityEntity city) {
//CityEntity via a converter class
return city;
}
@ModelAttribute("language")
public String getLanguage(@PathVariable String language) {
return language;
}
}
现在这两个公共属性将在模型对象中可用
@RequestMapping(value = "/{city}-{language}")
public class UserController extends AbstractController {
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public String user(@PathVariable String userId,
Model model) {...}
}
但我仍然需要多次放置@PathVariable。我正在寻找一种可以统一这些公共参数的方法(我想从我的控制器函数中删除它们,并将它们自动绑定到模式!)您需要这个注释来告诉spring这个变量是路径变量,而不是请求/命令对象。因此,只要不交换完整的参数解析程序,您就需要此注释。我同意您的看法,但为每个控制器方法添加这两个参数似乎不适合我。一种方法是创建一个特殊的过滤器,将这两个命令变量传递到请求参数中,我在问是否有办法做到这一点。@Ehab Al-Hakawati:我相信你试图通过大量复杂的工作取得一些小的进展,无论如何,我认为这是一项有趣的任务(从技术角度来看)。也许您应该看看Spring3.1中名为
RequestMappingHandlerMapping
的特性。有关简短介绍,请参见Spring Reverence 3.1章:“3.1.13基于新HandlerMethod的注释控制器处理支持类”
public abstract class AbstractController {
@ModelAttribute("currentCity")
public CityEntity getCurrentCity(@PathVariable CityEntity city) {
//CityEntity via a converter class
return city;
}
@ModelAttribute("language")
public String getLanguage(@PathVariable String language) {
return language;
}
}
@RequestMapping(value = "/{city}-{language}")
public class UserController extends AbstractController {
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public String user(@PathVariable String userId,
Model model) {...}
}