10.7、spring boot的web应用——访问静态资源
来源:互联网 发布:装修效果软件手机软件 编辑:程序博客网 时间:2024/05/28 06:07
一、原理
之前创建web工程的时候,直接把静态资源,比如html文件、图片等放在src/main/webapp目录下,在浏览器中是直接可以访问到这些静态资源的。但是在创建spring boot工程中,默认是没有创建webapp目录的,如果要把静态资源放在webapp目录下,需要手动在src/main/目录下创建一个webapp目录,然后把静态资源放在该目录下就可以,此时从浏览器中是可以直接访问到spring boot工程中的这些资源的。
但是现在在spring boot工程中,我们没必要去创建webapp,因为spring boot已经为我们创建好了默认的目录,只需要把静态资源放在默认目录下,浏览器就可以直接访问到。默认的静态资源目录配置在spring-boot-autoconfigure
jar包下的org.springframework.boot.autoconfigure.web包下ResourceProperties类,下面是源码
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
从源码中可以看出,静态资源存放的默认位置由4个目录,分别在根目录,即/src/main/resources/
目录下的/META-INF/resources/
、/resources/
、/static/
、/public/
目录下。下面以/static/
为例演示。
二、示例
创建目录如图所示
在/src/mian/resources/目录下创建ststic目录,该目录为静态资源默认存放位置。然后在static目录下分别创建一个css目录和一个js目录,以及一个login.html,然后在css目录下创建login.css,在js目录创建login.js。
login.html内容为:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><!-- 导入login.css和login.js --><link rel="stylesheet" href="css/login.css"><script src="js/login.js"></script></head><body> <h1>This is login page</h1></body></html>
login.css内容为:
body{color: red;}
只是把login.html文件中的body体中内容全部变为红色。
login.js内容为:
alert("Spring Boot!");
只是在显示login.html网页时,弹出一个警告。
spring boot的启动类为:
@SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
启动启动类,在浏览器中输入http://localhost:8080/login.html,回车,浏览器显示如下:
点击弹出的确定后,显示
从上面的一个示例中,可以看出,静态资源放在/static/目录下,浏览器可以直接访问。
三、自定义静态资源默认存储位置
spring boot工程默认情况下,浏览器可以直接访问到4个目录下的静态资源,但是若想浏览器访问自定义的目录,我们也可以手动指定。
还是从spring-boot-autoconfigure
jar包下的org.springframework.boot.autoconfigure.web包下ResourceProperties类中看,
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)public class ResourceProperties implements ResourceLoaderAware { private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" }; private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" }; private static final String[] RESOURCE_LOCATIONS; static { RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length + SERVLET_RESOURCE_LOCATIONS.length]; System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0, SERVLET_RESOURCE_LOCATIONS.length); System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length); } /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). */ /*staticLocations 数组变量指定了静态资源默认存储的位置,如果我们想自定义其它目录,只需要需要修改该变量指定的位置就可以*/ private String[] staticLocations = RESOURCE_LOCATIONS;
staticLocations 数组变量指定了静态资源默认存储的位置,如果我们想自定义其它目录,只需要需要修改该变量指定的位置就可以。我们可以在application.properties配置文件中修改静态资源存储位置,如下指定:
spring.resources.staticLocations=classpath:/mysources/
spring boot工程启动时,会默认加载该配置文件,然后修改静态资源默认存储位置,以后静态资源要放在/mysources/目录下。
- 10.7、spring boot的web应用——访问静态资源
- spring boot访问静态资源
- spring boot静态资源文件的访问以及自定义
- Spring Boot 设置静态资源访问
- Spring框架的WEB项目访问不到静态资源文件
- 10.6、spring boot的web应用——@ModelAttribute应用
- Spring-Data-Rest 构建Spring-boot Web应用—— rest风格访问JPA数据
- spring boot的静态资源加载
- (五)Spring Boot配置静态资源访问,整合Thymeleaf模板
- 为什么spring boot工程静态资源访问不到
- 为什么spring boot工程访问不到 静态资源
- 10.5、spring boot的web应用——更改服务器
- Spring Boot(六):如何配置静态资源的地址与访问路径
- 29、(知识篇)SpringMVC06 Spring jstlview国际化/访问/web-inf/下的jsp/访问静态资源
- Spring静态资源访问
- spring 静态资源访问
- 【Spring Boot】Spring Boot 静态资源处理
- web 配置可访问的静态资源
- 习题7(7.4)
- Java8之函数式接口
- 最短路径算法调研
- Android进阶九:TextView绘制原理之top,bottom,ascent,descent,baseline
- 关于数据库事务的隔离级别
- 10.7、spring boot的web应用——访问静态资源
- POJ 刷题系列:1062. 昂贵的聘礼
- 数据结构实验之查找五:平方之哈希表
- 看seo效果100天没起色的根源
- 习题7(7.5)
- python3 小数转换为整数的诡异问题
- Java常见面试题整理【2】
- CodeForces 17 D.Notepad(指数循环定理)
- 根据属性来选择xml节点