SpringMVC如何高效访问静态资源?三种方法速览!
- 内容介绍
- 文章标签
- 相关推荐
本文共计672个文字,预计阅读时间需要3分钟。
方案一:激活Tomcat的defaultServlet来处理静态文件
- 配置文件:`web.xml`- 配置内容: xml defaultServlet org.apache.catalina.servlets.DefaultServlet debug 0 listings false maxFiles 10 1 defaultServlet /static/*
- 修改DispatcherServlet的配置,使其先拦截静态资源请求,再处理其他请求:
java @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer {
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
@Override public void configureHandlerMappings(HandlerMappingBuilder mappingBuilder) { mappingBuilder.pathMatchers(/static/**).mapping(new HandlerMapping() { @Override public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { return new HandlerExecutionChain(new SimpleUrlHandlerMapping() { @Override protected HandlerExecutionChain getHandlerInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { return new HandlerExecutionChain(new DefaultServletHandler()); } }); } }); } }
gistfile1.txt方案一:激活Tomcat的defaultServlet来处理静态文件
本文共计672个文字,预计阅读时间需要3分钟。
方案一:激活Tomcat的defaultServlet来处理静态文件
- 配置文件:`web.xml`- 配置内容: xml defaultServlet org.apache.catalina.servlets.DefaultServlet debug 0 listings false maxFiles 10 1 defaultServlet /static/*
- 修改DispatcherServlet的配置,使其先拦截静态资源请求,再处理其他请求:
java @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer {
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
@Override public void configureHandlerMappings(HandlerMappingBuilder mappingBuilder) { mappingBuilder.pathMatchers(/static/**).mapping(new HandlerMapping() { @Override public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { return new HandlerExecutionChain(new SimpleUrlHandlerMapping() { @Override protected HandlerExecutionChain getHandlerInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { return new HandlerExecutionChain(new DefaultServletHandler()); } }); } }); } }
gistfile1.txt方案一:激活Tomcat的defaultServlet来处理静态文件

