我只是迁移到spring版本的5.0.1.RELEASE
,但突然在eclipse中,STS WebMvcConfigurerAdapter被标记为不推荐使用。
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
// to serve static .html pages...
registry.addResourceHandler("/static/**").addResourceLocations("/resources/static/");
}
....
}
我怎么能移除这个!
发布于 2017-11-29 13:05:41
从Spring 5开始,您只需要实现接口WebMvcConfigurer
public class MvcConfig implements WebMvcConfigurer {
这是因为Java 8在接口上引入了默认方法,这些方法涵盖了WebMvcConfigurerAdapter
类的功能。
见这里:
发布于 2018-07-26 19:35:30
我现在一直在开发名为Springfox
的Swagger等效文档库,我发现在Spring5.0.8(目前正在运行)中,接口WebMvcConfigurer
已经由WebMvcConfigurationSupport
类实现,我们可以直接扩展该类。
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
public class WebConfig extends WebMvcConfigurationSupport { }
这就是我如何使用它来设置我的资源处理机制如下-
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
发布于 2018-08-09 09:06:10
在春季,每个请求都将通过DispatcherServlet.为了避免通过DispatcherServlet(Front )请求静态文件,我们配置了MVC静态内容。
Spring3.1..引入了ResourceHandlerRegistry来配置ResourceHttpRequestHandlers,用于服务来自类路径、WAR或文件系统的静态资源。我们可以在web上下文配置类中以编程方式配置ResourceHandlerRegistry。
** pattern to the ResourceHandler, lets include the
foo.jsresource located in the **
webapp/js/`**目录** pattern to the ResourceHandler, lets include the
foo.htmlresource located in the **
webapp/resources/`**目录@Configuration
@EnableWebMvc
public class StaticResourceConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
registry.addResourceHandler("/resources/static/**")
.addResourceLocations("/resources/");
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
}
}
XML配置
<mvc:annotation-driven />
<mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/"
cache-period="60"/>
如果文件位于WAR的webapp/resources文件夹中,则为Spring Boot MVC静态内容。
spring.mvc.static-path-pattern=/resources/static/**
https://stackoverflow.com/questions/47552835
复制相似问题