mvc:resources 的注解配置替换 - Spring [英] Annotation Configuration Replacement for mvc:resources - Spring
问题描述
我正在尝试升级我的 spring mvc 项目以利用新的注释并摆脱我的 xml.以前我在我的 web.xml
中加载我的静态资源:
I'm trying to upgrade my spring mvc project to utilize the new annotations and get rid of my xml. Previously I was loading my static resources in my web.xml
with the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
现在,我正在使用 WebApplicationInitializer
类和 @EnableWebMvc
注释来启动我的服务,而没有任何 xml 文件,但似乎无法弄清楚如何加载我的资源.
Now, I'm utilizing the WebApplicationInitializer
class and @EnableWebMvc
annotation to startup my service without any xml files, but can't seem to figure out how to load my resources.
是否有注释或新配置可以在无需使用 xml 的情况下将这些资源拉回?
Is there an annotation or new configuration to pull these resources back in without having to use xml?
推荐答案
对于 Spring 3 &4:
一种方法是让您的配置类扩展WebMvcConfigurerAdapter
,然后重写以下方法:
One way to do this is to have your configuration class extend WebMvcConfigurerAdapter
, then override the following method as such:
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
这篇关于mvc:resources 的注解配置替换 - Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!