SpringBoot2.X中的静态资源访问失效
今天开始使用SpringBoot写项目,于是先让其能够访问静态资源,但是配置半天也不能访问我的helloWorld页面,原来,在SpringBoot2.x中,一下静态资源路径不生效了。
classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。
springboot2.x以后,支持jdk1.8,运用了jdk1.8的一些特性。jdk1.8支持在接口中添加default方法,而此方法具有具体的方法实现。静态资源和拦截器的处理,不再继承“WebMvcConfigurerAdapter”方法。而是直接实现“WebMvcConfigurer”接口。通过重写接口中的default方法,来增加额外的配置。
要想能够访问静态资源,请配置WebMvcConfigurer
package io.guangsoft.web.config; import io.guangsoft.common.interceptor.EncodingInterceptor;
import io.guangsoft.common.security.shiro.interceptor.PermissionInterceptorAdapter;
import io.guangsoft.common.utils.SpringContextHolder;
import io.guangsoft.web.interceptor.WebInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration
public class InterceptorConfig implements WebMvcConfigurer { /**
* 编码拦截器
*/
@Bean
public HandlerInterceptor encodingInterceptor() {
EncodingInterceptor encodingInterceptor = new EncodingInterceptor();
return encodingInterceptor;
} /**
* 安全验证拦截器
*
* @return
*/
@Bean
public PermissionInterceptorAdapter permissionInterceptorAdapter() {
PermissionInterceptorAdapter permissionInterceptorAdapter = new PermissionInterceptorAdapter();
return permissionInterceptorAdapter;
} /**
* 静态资源拦截器
*/
@Bean
public WebInterceptor webInterceptor() {
WebInterceptor webInterceptor = new WebInterceptor();
return webInterceptor;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
//编码拦截器
registry.addInterceptor(encodingInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
//安全验证拦截器
registry.addInterceptor(permissionInterceptorAdapter()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
//web拦截器
registry.addInterceptor(webInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
} /**
* 添加静态资源文件,外部可以直接访问地址
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//第一个方法设置访问路径前缀,第二个方法设置资源路径
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//添加对上传文件的直接访问
Environment env = SpringContextHolder.getBean(Environment.class);
String uploadFilePath = env.getProperty("upload-file-path");
registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadFilePath);
} }
SpringBoot2.X中的静态资源访问失效的更多相关文章
- SpringBoot中的静态资源访问
一.说在前面的话 我们之间介绍过SpringBoot自动配置的原理,基本上是如下: xxxxAutoConfiguration:帮我们给容器中自动配置组件: xxxxProperties:配置类来封装 ...
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层
2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static /public /resourc ...
- SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆
在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...
- 7.Spring MVC静态资源访问
在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被DispatcherServlet所拦 ...
- Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结
上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...
- spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK
- 【SpringMVC】静态资源访问的问题
在项目中经常会用到一些静态的资源,而一般我们在配置SpringMVC时会让SpringMVC接管所有的请求(包括静态资源的访问), 那么我们怎样才能最简单的来配置静态资源的访问呢? 一,在web.xm ...
- 关于linux下部署JavaWeb项目,nginx负责静态资源访问,tomcat负责处理动态请求的nginx配置
1.项目的运行环境 linux版本 [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...
随机推荐
- javascript_06-控制流程
流程控制 程序的三种基本结构 顺序结构 选择结构 循环结构 判断语句 if 语法: if(condition){ //todo }else if{ //todo }else{ //todo } var ...
- mongodb副本集和分片存储理论整理
目录 理论概述 一.各种集群简述 二.原理 主从复制 Mongodb副本集 理论概述 一.各种集群简述 mongodb有三种集群搭建方式: 分片:sharding.指为处理大量数据,将数据分开存储,不 ...
- linux网络编程之system v信号量(一)
今天起,学习信号量相关的知识,下面开始: 关于信号量,在前面已经介绍过了,这里回顾一下: 通过上面的描述,很容易就能想到信号量的一上数据结构: 下面再来回顾一下P.V原语: 所谓的原语就是指这段代码是 ...
- C# 中自定义配置
微软在ConfigurationManager类里面为我们提供了AppSetting和ConnectionStrings 两个常用配置, 但是有时候我们需要自定的配置,例如 <image lef ...
- msaa mrt load store action unity
unity buildin renderpipeline 和lightweight rp 对于开了msaa的rt 的load store action设置失效 buildin的时候set render ...
- Spring MVC框架及标签库
1.Spring MVC技术 1. 当DispatcherServlet接到请求时,他先回查找适当的处理程序来处理请求.DispatcherServlet通过一个或者多个处理程序映射,将每个请求映射到 ...
- 淘宝上的大智慧L2数据,月卡最便宜是8元钱,这个也可以获取BBD、DDX等数据!
Want:从顶牛股网上下载DDX数据. 1.下载历史DDE数据:获取最近120个交易日的DDE数据 #define SFURL_DNG_SINGLEL"http://www.dingniug ...
- [2019南昌邀请赛网络赛D][dp]
https://nanti.jisuanke.com/t/38223 Xiao Ming recently indulges in match stick game and he thinks he ...
- 突然萌发关于 redis 的想法(1)
本来昨天就打算写这篇了,但是熬到忘了,至于为什么要写这个是因为我昨天在写 redis 的时候突然想到的 注:此篇文章并没有讲解 redis 内部的使用 或 如何使用redis写代码,等等..仅仅只是突 ...
- mongoose 5.0 链接数据库 代码保存
const mongoose = require('mongoose'); const dbSrc = 'mongodb://localhost/douban-trailer' mongoose.Pr ...