我想升级我的Spring MVC的项目,以利用新的注释和摆脱我的XML。 以前我是我的加载静态资源在我web.xml
与线:
<mvc:resources mapping="/resources/**" location="/resources/" />
现在,我利用WebApplicationInitializer
类和@EnableWebMvc
注释启动我的服务没有任何的XML文件,但似乎无法弄清楚如何加载我的资源。
是否有一个注释或新配置回拉这些资源,而不必使用XML?
春季3和4:
这样做的一个方法就是让您的配置类扩展WebMvcConfigurerAdapter
,然后覆盖下面的方法,例如:
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
春天5
由于弹簧5的,要做到这一点正确的方法是简单地实现WebMvcConfigurer接口。
例如:
@Configuration
@EnableWebMvc
public class MyApplication implements WebMvcConfigurer {
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
见弃用消息: WebMvcConfigurerAdapter