SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的。这篇文章就来讲讲这方面问题,也是项目中的坑。

先看看项目结构,这个项目主要是开发商管理系统,包含了开发商信息,开发商订单等模块。主要介绍 resources 目录,static 静态资源目录,包含 js,css 等。templates 目录主要是页面。SpringBoot 和 thymeleaf 是官方推荐,也是默认集成。

划掉了的都是不需要的,先来看看我的 controller 类

package com.example.demo.controller;

import com.example.demo.entity.InventoryRequestEntity;
import com.example.demo.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
* DemoController
* TODO
* rockcode
* 2018/6/25-9:47
*/
@Controller
public class DemoController { @RequestMapping("/login")
public String login() {
System.out.println(">>>>>>>>>>>>>>>>>>>>>login>>>>>>>>>>>>>>>>>>>>>>");
return "index";
} @RequestMapping("/")
public String index() {
System.out.println(">>>>>>>>>>>>>>>>>>>>>/>>>>>>>>>>>>>>>>>>>>>>");
return "index";
} }

SpringBoot 版本

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository 2.0.3.RELEASE -->
</parent>

启动项目,先来访问 localhost:8888/,没问题可以访问 templates 下面的 index.html 页面。这说明,SpringBoot 默认去 templates 目录寻找页面,再来看看页面上脚本和样式引入。

我的所有 js 和 css 都放在 resources/static 目录下面,但是我引入的时候不用加 static 一样成功,这说明 SpringBoot 默认去 static 目录引入脚本和样式,下面是成功页面导航栏。

关于上面端口 8888,可以在 application.properties 里面配置,server.port = 8888。

到这里都没有问题,但是出于安全的考虑,我必须加上拦截器,如果没有登录,那必须去登录才可以访问。先来看看 1.5.4.RELEASE 版本拦截器。

package com.example.demo.config;

import com.example.demo.interceptor.DemoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*; /**
* WebConfig
* TODO
* rockcode
* 2018/7/4-10:21
*/ @Configuration
public class WebConfig extends WebMvcConfigurerAdapter{ @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/");
}
}
package com.example.demo.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.UrlPathHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* DemoInterceptor
* TODO
* rockcode
* 2018/7/4-10:02
*/
public class DemoInterceptor extends HandlerInterceptorAdapter { private UrlPathHelper urlPathHelper = new UrlPathHelper(); @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("DemoInterceptor....验证用户名,密码是否正确....");
response.sendRedirect("/login");
return true;
}
}

现在来访问 localhost:8888/,拦截器会拦截 /,然后跳转到 /login,让其去做验证等等事情。下面是成功界面。但是如果把版本换成 2.0.3.RELEASE,你会发现页面变得很乱,样式和脚本没办法加载进来。

原因在 1.X 和 2.X 静态资源访问方式变了,1.X 是默认 static 目录,但是 2.X 需要显示去说明。

我们来看看 2.X 的拦截器有什么不同,首先 WebMvcConfigurerAdapter 已经过时,要用 WebMvcConfigurationSupport 类,而且需要实现 addResourceHandlers 方法。

package com.example.demo.config;

import com.example.demo.interceptor.DemoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /**
* WebConfig
* TODO
* rockcode
* 2018/7/4-10:21
*/ @Configuration
public class WebConfig extends WebMvcConfigurationSupport { @Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/login").excludePathPatterns("/static/**");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}

指定了静态文件目录,在页面引用必须加上 static,例如  <link rel="stylesheet" href="static/css/bootstrap.min.css"/> 等。

这其中的原因,可以去看看 WebMvcConfigurationSupport 和 WebMvcConfigurerAdapter 源码。

聊聊、SpringBoot 静态资源访问的更多相关文章

  1. springboot 静态资源访问,和文件上传 ,以及路径问题

    springboot 静态资源访问: 这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...

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

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

  3. springmvc、springboot静态资源访问配置

    如何访问项目中的静态资源? 一.springmvc springmvc中访问静态资源,如果DispatcherServlet拦截的为"",那么静态资源的访问也会交给Dispatch ...

  4. SpringBoot静态资源访问

    在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置: Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static ...

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

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

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

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

  7. springboot + thymeleaf静态资源访问404

    在使用springboot 和thtmeleaf开发时引用静态资源404,静态资源结如下: index.html文件: <!DOCTYPE html> <html xmlns:th= ...

  8. SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径

    Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...

  9. springboot配置静态资源访问路径

    其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...

随机推荐

  1. (转载)office 2003 gaozhi.msi 缺失提示问题修复

    某些GHOST版win7,自带office 2003,每次启动word,它都会提示"稿纸没安装"云云,找不到那个文件.可是我搜遍了硬盘,确实没有那个文件.每次都要点取消,这个提示才 ...

  2. java Vamei快速教程06 组合

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经尝试去定义类.定义类,就是新建了一种类型(type).有了类,我们接着构造 ...

  3. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  4. SpringBoot操作MongoDB实现增删改查

    本篇博客主讲如何使用SpringBoot操作MongoDB. SpringBoot操作MongoDB实现增删改查 (1)pom.xml引入依赖 <dependency> <group ...

  5. 调试工具DEBUG的使用(8086)

    有关CPU和存储单元的概念在前一节我们已经了解,那么如何观察实际机器内部的情况呢?能不能看到具体的寄存器.标志.存储单元的内容呢?可不可以修改和控制它们呢? DEBUG这个有力工具,就可以深入到机器内 ...

  6. Problem A: 李白打酒

    Problem A: 李白打酒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 825  Solved: 373[Submit][Status][Web ...

  7. Finite Encyclopedia of Integer Sequences(找规律)

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB提交: 375  解决: 91[提交] [状态] [讨论 ...

  8. kubernetes-服务发现service(九)

    service •防止Pod失联    •定义一组Pod的访问策略    •支持ClusterIP,NodePort以及LoadBalancer三种类型    •Service的底层实现主要有ipta ...

  9. javascrit中“字符串为什么可以调用成员”

    <script> var title = "this is title"; console.log(title.substr(0,5));   //字符串为什么可以调用 ...

  10. Bootstrap 历练实例 - 折叠(Collapse)插件方法

    方法 下面是一些折叠(Collapse)插件中有用的方法: 方法 描述 实例 Options:.collapse(options) 激活内容为可折叠元素.接受一个可选的 options 对象. $(' ...