今天开始使用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. C++——友元 friend

    人类社会的friend VS C++世界的friend 现实世界中,我们自己很多物品,朋友是可以使用的,但是陌生人就不行.那么money,朋友可以随便拿吗?这要是你和friend的关系深浅而定.人类社 ...

  2. 网上的JAVA语言的某个测试框架

    https://github.com/wenchengyao/testLJTT.git 使用maven打包,mvn clean install 在运行的时候,java -jar testLJTT.ja ...

  3. MySQL 中 关于 find_in_set 用法

    在项目中用的是 EF.MySQL,在查询中用到某一个字段的值在某几个中.一开始想到的是 in. 但是,这个字段是 int 类型的,传入的参数需要用“,”分隔.这样传入的就是字符型的. 这样的结果是:本 ...

  4. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  5. 13 Vue事件处理

    监听事件 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码. 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码. 示例: ...

  6. git致命错误汇总

    1.  拒绝合并无关历史 fatal: refusing to merge unrelated histories 解决办法: git pull origin master --allow-unrel ...

  7. 关于ORA-12154: TNS:could not resolve the connect identifier specified的问题

    问题:net configuration assistant测试连接可以连接.但是PLSQL连接不了,报ORA-12154: TNS:could not resolve the connect ide ...

  8. 38 | 都说InnoDB好,那还要不要使用Memory引擎?

    我在上一篇文章末尾留给你的问题是:两个 group by 语句都用了 order by null,为什么使用内存临时表得到的语句结果里,0 这个值在最后一行:而使用磁盘临时表得到的结果里,0 这个值在 ...

  9. 国庆集训 Day1 T2 生成图 DP

    国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...

  10. grafana+mysql忘记admin密码解决方法

    通过mysql修改admin的密码. 我之前注册了一个用户,所以我直接复制lvusyy这个用户的密码即可. update user inner join (select password,salt,r ...