Spring 5 - 如何提供静态资源 [英] Spring 5 - How to provide static resources

查看:184
本文介绍了Spring 5 - 如何提供静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网络应用程序中提供静态资源,我尝试了:

I am trying provide static resources in my web application and I tried:

@SuppressWarnings("deprecation")
@Bean
WebMvcConfigurerAdapter configurer(){
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").
                      addResourceLocations("classpath:/static/");
        }
    };
}

但是在Spring 5中不推荐使用WebMvcConfigurerAdapter。如何现在访问静态资源?

BUT WebMvcConfigurerAdapter is deprecated in Spring 5. How can I access the static resources now?

推荐答案

Spring 5 - 静态资源

来自文档:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/public", "classpath:/static/")
                        .setCachePeriod(31556926);
        }

}

这篇关于Spring 5 - 如何提供静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆