我正在尝试升级我的 spring mvc 项目以利用新的注释并摆脱我的 xml。以前我在我的 web.xml
中加载我的静态资源:
<mvc:resources mapping="/resources/**" location="/resources/" />
现在,我正在利用 WebApplicationInitializer
类和 @EnableWebMvc
注释来启动我的服务,而无需任何 xml 文件,但似乎无法弄清楚如何加载我的资源。
是否有注释或新配置可以将这些资源拉回而不必使用 xml?
最佳答案
Spring 3 和 4:
一种方法是让您的配置类扩展 WebMvcConfigurerAdapter
,然后像这样覆盖以下方法:
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
关于java - mvc :resources - Spring 的注解配置替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861720/