10.7、spring boot的web应用——访问静态资源

一、原理
之前创建web工程的时候,直接把静态资源,比如html文件、图片等放在src/main/webapp目录下,在浏览器中是直接可以访问到这些静态资源的。但是在创建spring boot工程中,默认是没有创建webapp目录的,如果要把静态资源放在webapp目录下,需要手动在src/main/目录下创建一个webapp目录,然后把静态资源放在该目录下就可以,此时从浏览器中是可以直接访问到spring boot工程中的这些资源的。
但是现在在spring boot工程中,我们没必要去创建webapp,因为spring boot已经为我们创建好了默认的目录,只需要把静态资源放在默认目录下,浏览器就可以直接访问到。默认的静态资源目录配置在spring-boot-autoconfigurejar包下的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-autoconfigurejar包下的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/目录下。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值