今天开始使用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. 【leetcode】544. Output Contest Matches

    原题 During the NBA playoffs, we always arrange the rather strong team to play with the rather weak te ...

  2. Android 通过资源名,获取资源ID

    有时候我们知道一个图片的文件名,我们需要知道在R文件中,该资源的ID,使用如下方法: public static int getIdByName(Context context, String cla ...

  3. Linux命令——yum

    翻译自:20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management 前言 本篇文章将介绍如何使用RedHat开 ...

  4. 制作一个简单的部门员工知识分享的python抽取脚本

    需求: 基于公司的文化和公司部门间以及员工之间的工作需求状态,或者想要了解某一些技能.专业方面的知识需求.促进并提高员工们的技能认知和技术水平. 详细代码如下: 先说一下存入csv表格的表头字段: 1 ...

  5. 利用videojs自动播放下一个

    利用videojs自动播放下一个 一.总结 一句话总结: 在视频放完的ended方法里面,指定video的src,然后this.play()放视频就好 vue来控制视频的链接也是蛮不错的 this.o ...

  6. Oracle使用游标查询所有数据表备注

    功能作用:应用对应的SQL语句,能方便快速的查询Oracle数据库指定用户的所有用户表说明,快速知道每个数据表是做什么的,方便写文档和方案. 运行环境:搭建好Oracle数据库,并使用PQ/SQL D ...

  7. Spring MVC 学习笔记(一)

    • 1.SpringMVC概述 • 2.SpringMVC的HelloWorld • 3.使用@RequestMapping映射请求 • 4.映射请求参数&请求头 • 5.处理模型数据 • 6 ...

  8. Vue之nextTick()

    我们有时候操作 DOM,是想在 data 数据变更的时候进行操作. 那么,我们应该怎么做呢? index.html <!DOCTYPE html> <html lang=" ...

  9. python - django (查询、聚合、分组)

    # """ ---- 正向查询按字段,反向查询按表名 一: 一对多 正向查询:(字段对象.关联表.查询字段) x_obj = models.Book.objects.fi ...

  10. 服务器nginx配置显示文件而不是下载

    有时候在服务器上配置某些文件比如TXT文件,在浏览器打开的时候,会弹出下载.如何只让他在浏览器中显示,而不是下载呢.在nginx配置文件中添加一行代码 add_header Content-Type ...