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_02-变量
变量 var number = 5; number = 5; //与上面一样的效果,语法没错误,但是不规范 var:关键字,变量的意思. 变量可以立即赋值,也可以稍后赋值. 堆和栈 内存中有两个区域, ...
- 【DRF框架】序列化组件——ModelSerializer
ModelSerializer 1.ModelSerializer类似于ModelForm 2.根据模型自动生成一组字段 3.自带实现了.update()以及.create()方法 ModelSeri ...
- Windows 网络凭证
前言 单位内部,员工之间电脑免不了要相互访问(eg:访问共享文件夹).这就引出网络凭证的概念,即你用什么身份访问对端计算机. 实验环境 创建共享文件夹 WinSrv 2008上新建的文件夹shared ...
- 制作一个简单的部门员工知识分享的python抽取脚本
需求: 基于公司的文化和公司部门间以及员工之间的工作需求状态,或者想要了解某一些技能.专业方面的知识需求.促进并提高员工们的技能认知和技术水平. 详细代码如下: 先说一下存入csv表格的表头字段: 1 ...
- python-----图像去重(imagededup)
安装库: pip install imagededup 安装可能遇到的问题参考: Cannot uninstall 'wrapt'. It is a distutils installed proje ...
- 基于Java+Selenium的WebUI自动化测试框架(六)---浏览器初始化
本篇我们来讨论,如何写一个浏览器初始化的类.在写之前,先思考一下,我们需要一个什么样的初始化? 先来看看使用原生的Java + selenium是怎么做的.(以firefox为例) System.se ...
- 1260:【例9.4】拦截导弹(Noip1999)
题目来源:http://ybt.ssoier.cn:8088/problem_show.php?pid=1260 1260:[例9.4]拦截导弹(Noip1999) 时间限制: 1000 ms ...
- C#常用控件和属性
目录1.窗体(Form)2.Label (标签)控件3.TextBox(文本框)控件4.RichTextBox控件5.NumericUpDown控件6.Button(按钮)控件7.GroupBox(分 ...
- Django中出现:TemplateDoesNotExist at
setting文件中加入:
- git 廖雪峰博客笔记
其他参考 http://www.softwhy.com/article-8498-1.html 本地模拟多用户来学习 参考 其实就是克隆两个目录,两个目录的git config 不同的名字和邮箱, ...