今天开始使用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中的静态资源访问失效的更多相关文章

  1. SpringBoot中的静态资源访问

    一.说在前面的话 我们之间介绍过SpringBoot自动配置的原理,基本上是如下: xxxxAutoConfiguration:帮我们给容器中自动配置组件: xxxxProperties:配置类来封装 ...

  2. IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404

    IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...

  3. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  4. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...

  5. 7.Spring MVC静态资源访问

    在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被DispatcherServlet所拦 ...

  6. Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

    上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...

  7. spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描

    1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK

  8. 【SpringMVC】静态资源访问的问题

    在项目中经常会用到一些静态的资源,而一般我们在配置SpringMVC时会让SpringMVC接管所有的请求(包括静态资源的访问), 那么我们怎样才能最简单的来配置静态资源的访问呢? 一,在web.xm ...

  9. 关于linux下部署JavaWeb项目,nginx负责静态资源访问,tomcat负责处理动态请求的nginx配置

    1.项目的运行环境 linux版本 [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...

随机推荐

  1. 元组和range

    元组 只读列表,不支持增 删 改:但是元组里的列表可以增删改 元组其实就是通过逗号(,)设定的,和小括号并没有什么必然的关系,所以当元组只有一个元素的时候,需要在元素后加个逗号 存储大量数据,有序.不 ...

  2. 异常-Phoenix HBASE Last region should end with an empty key. You need to create a new region and regioninfo in HDFS to plug the hole

    1 详细异常信息 RROR: There is a hole in the region chain between \x03\x00\x00\x00\x00\x00\x00\x00\x00 and ...

  3. Python高阶用法总结

    目录 1. lambda匿名函数 1.1 函数式编程 1.2 应用在闭包 2. 列表解析式 3. enumerate内建函数 4. 迭代器与生成器 4.1 迭代器 4.3 生成器 5. 装饰器 前言: ...

  4. windows cmd命令学习

    tasklist|findstr "py"

  5. windows RabbitMQ Server 环境配置中的一些坑

    原文:https://blog.csdn.net/lindonglian/article/details/55805637 RabbitMQ的服务端基于Erlang语言编写,要在机器上安装Rabbit ...

  6. pringBoot2.0启用https协议

    SpringBoot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码. 我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用h ...

  7. 运输层8——TCP运输连接管理

    目录 1. TCP的连接建立 2. TCP的连接释放 写在前面:本文章是针对<计算机网络第七版>的学习笔记 运输层1--运输层协议概述 运输层2--用户数据报协议UDP 运输层3--传输控 ...

  8. JS多线程之Web Worker

    什么是Web Worker web worker 是运行在后台的 JavaScript,不会影响页面的性能. 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web wor ...

  9. Jquery 操作DOM元素

    一.文本输入框: text <input type=”text” value=”99.com” size=12 id=”input1” /> 1.获取文本值: $("#input ...

  10. vue 安装 ‘node-sass’ 运行报错:ERROR in Cannot find module 'node-sass'

    好像是由于cnpm安装导致.执行下面的安装代码,或者使用npm 安装node-sass cnpm install node-sass@latest