1. SpringBoot从ServletContext根目录的webapp目录下访问静态资源, 注意目录名称必须是webapp。
2. 使用maven构建SpringBoot的名叫spring-boot-static-resources2项目
3. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjbs</groupId>
<artifactId>spring-boot-static-resources2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<!-- 修改jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4. 在src/main下创建webapp目录, webapp目录下创建index.html; 在webapp目录下创建images目录, images目录下创建java.jpg
5. index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>静态资源访问方式二</title>
</head>
<body>
静态资源访问方式二<hr />
<img alt="" src="images/java.jpg">
</body>
</html>
6. 创建App.java启动类
package com.bjbs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* springBoot启动类
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
7. 启动项目, 使用浏览器访问index.html
8. 使用浏览器直接访问java.jpg