Spring Boot中静态资源访问的默认配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/russle/article/details/81262799

传统的Java Web项目中,所有的静态文件和页面都是放在WebContent目录下。但在Spring Boot项目中,静态资源和页面文件都统一放在src/main/resources/static或者src/main/resources/public目录下对应的文件夹中。一般src/main/resources/static目录用于存放各类静态资源文件,例如css、js和image等。src/main/resources/templates用于存放页面文件,例如html,jsp等。如果我们不使用thyleleaf、FreeMaker、Velocity、JSP等, 默认的静态资源访问情况如下文所示。

完整的代码在这里,欢迎加星、fork。

1, 配置
因为我们不使用thyleleaf、FreeMaker、Velocity、JSP等,所以pom文件非常简单。本示例工程使用spring boot 2.0.0。pom文件中最重要的是spring-boot-starter-web依赖。其余的依赖都是额外附属的。例如webjars-locator-core,bootstrap,jquery是为了在html使用一些bootstrap和jquery的东西,但是又不想直接访问这些类库的对应资源URL,而是在java程序中包含了一份。lombok依赖是为了省略set get方法。

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- springboot 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2, 通过html访问
静态资源文件放在src/main/resources/static下对应得目录中。如图所示。

这里写图片描述

如果不使用thyleleaf、FreeMaker、Velocity,不讲将index.html页面放在在src/main/resources/templates,只能放在src/main/resources/static目录下面或者src/main/resources/public目录下面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Hello 静态资源访问演示程序</title>
    <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>-->
    <link href="/css/main.css" rel="stylesheet">
    <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="/webjars/jquery/jquery.min.js"></script>
    <script src="/hello.js"></script>
</head>


<body>
<div>
    <img src="/img/cloud01.jpg"></img>
    <p class="user-id">The user id is </p>
    <p class="user-name">The user name is </p>
</div>
</body>
</html>

3, 效果演示

访问css和js文件的效果
这里写图片描述

访问jpg图片的效果
这里写图片描述

4, 源代码分析

我们通过查看sping boot的源代码可以发现。系统默认我们配置了static和public路径。重点是”classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/”

public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private String[] staticLocations;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private final ResourceProperties.Cache cache;
...

启动日志图(index.html文件放在public目录下面了)
这里写图片描述

猜你喜欢

转载自blog.csdn.net/russle/article/details/81262799