一、原理
之前创建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的启动类为:
@SpringBootApplication
public 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/目录下。