博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot mvc系列-静态资源配置与MappingHandler拦截器
阅读量:7062 次
发布时间:2019-06-28

本文共 2098 字,大约阅读时间需要 6 分钟。

静态资源配置

Spring Boot 默认将 /** 所有访问映射到以下目录:

classpath:/staticclasspath:/publicclasspath:/resourcesclasspath:/META-INF/resources

如果需要自定义映射目录,可以继承WebMvcConfigurerAdapter或WebMvcConfigurationSupport,以后者为例,如下:

@Configurationpublic class WebConfig extends WebMvcConfigurationSupport {        @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        //将所有/static/** 访问都映射到classpath:/static/ 目录下        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/static/resources/");        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");        registry.addResourceHandler("/font/**").addResourceLocations("classpath:/static/font/");        registry.addResourceHandler("/themes/**").addResourceLocations("classpath:/static/themes/");    }}

如果使用了拦截器HandlerInterceptor,好像覆盖addResourceHandlers方法,似乎excludePathPatterns并没有生效,不覆盖的话前台会报404。

拦截器配置 

同样在WebConfig中配置,如下:

package com.hundsun.ta.aop.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import com.hundsun.ta.interceptor.SecurityInteceptor;@Configurationpublic class WebConfig extends WebMvcConfigurationSupport {    // 需要注意的是HandlerInteceptor必须通过@Bean配置,直接添加@new SecurityInteceptor()会导致依赖类未注入    @Bean    SecurityInteceptor securityInteceptor() {        return new SecurityInteceptor();    }    @Override    protected void addInterceptors(InterceptorRegistry registry) {        registry.addInterceptor(securityInteceptor()).excludePathPatterns("/css/**", "/js/**", "/font/**", "/images/**", "/resources/**", "/themes/**");        super.addInterceptors(registry);    }}

 

转载地址:http://ltnll.baihongyu.com/

你可能感兴趣的文章
Exchange 2013部署系列之(八)邮箱、通讯组创建及规划
查看>>
贪心初步-A - Doing Homework again
查看>>
[CTO札记]盛大文学公司名称对联
查看>>
AD DS最佳实践分析程序(BPA)应用实例---扫描并归档结果
查看>>
RAC 开启gsd和oc4j服务
查看>>
MYSQL 动态变量赋值不对的情况
查看>>
Android应用程序组件Content Provider的启动过程源代码分析(7)
查看>>
Nginx实战基础篇一 源码包编译安装部署web服务器
查看>>
IDEA编译时候出现问题:代码不提示错误,编译时出错解决办法
查看>>
简述WebService与.NET Remoting的区别及适应场合
查看>>
Oracle EBS 如何生成trace文件
查看>>
[存档]名词解释
查看>>
老树新芽,在ES6下使用Express
查看>>
【实验】修改数据文件名字的三种途径
查看>>
Linux内核中_IO,_IOR,_IOW,_IOWR宏的用法与解析【转】
查看>>
把用户加入sudo
查看>>
heroku 上部署node.js的几个注意点
查看>>
How to: Define a Conversion Operator [msdn]
查看>>
android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播...
查看>>
MFC连接数据库
查看>>