注释配置替换mvc:resources - Spring [英] Annotation Configuration Replacement for mvc:resources - Spring

查看:257
本文介绍了注释配置替换mvc:resources - Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想升级我的spring mvc项目,以利用新的注释,摆脱我的xml。以前,我在 web.xml 中加载我的静态资源:

 < mvc:resources mapping =/ resources / **location =/ resources //> 

现在,我使用 WebApplicationInitializer 类和 @EnableWebMvc 注释启动我的服务没有任何xml文件,但似乎无法找出如何加载我的资源。



有没有注释或新配置,可以将这些资源重新插入而不必使用xml?

一种方法是让你的配置类扩展 WebMvcConfigurerAdapter ,然后覆盖下面的方法:

  @Override 
public void addResourceHandlers(最终的ResourceHandlerRegistry注册表){
registry.addResourceHandler(/ resources / **)。 addResourceLocations(/ resources /);
}


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/" /> 

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.

Is there an annotation or new configuration to pull these resources back in without having to use xml?

解决方案

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屋!

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