基础10-Web开发基础

Spring Boot对静态资源的映射规则

WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers方法,该方法代码如下:

public void addResourceHandlers(ResourceHandlerRegistry registry) {

            if (!this.resourceProperties.isAddMappings()) {

                logger.debug("Default resource handling disabled");

            } else {

                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();

                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();

                if (!registry.hasMappingForPattern("/webjars/**")) {

                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));

                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();

                if (!registry.hasMappingForPattern(staticPathPattern)) {

                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));

                }

            }

        }

 

private Optional<Resource> getWelcomePage() {

            String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());

            return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();

        }

 

        private Resource getIndexHtml(String location) {

            return this.resourceLoader.getResource(location + "index.html");

        }

①所有/webjars/目录下的文件,都会去classpath:/META-INF/resources/webjars/下找资源;

这里的resources目录相当于ROOT目录,可以这样访问JS文件:http://127.0.0.1:8080/webjars/jquery/3.5.1/jquery.js

备注:webjars,是一门技术,负责把静态资源(例如:jquery)添加到jar包中,且支持Maven的<dependency/>,可以像引用jar一样去引用静态资源,下面是官网链接:https://www.webjars.com/

<dependency>

<groupId>org.webjars</groupId>

 <artifactId>jquery</artifactId>

 <version>3.5.1</version>

</dependency>

②跟踪String staticPathPattern = this.mvcProperties.getStaticPathPattern();代码,我们发现代码引用的是staticPathPattern变量,而该变量在WebMvcProperties构造方法里被初始化为"/**";

跟踪this.resourceProperties.getStaticLocations()代码,我们发现变量staticLocations取值来自于CLASSPATH_RESOURCE_LOCATIONS变量,而该变量是一个数组:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"};

我来详细说明下,我们以http://127.0.0.1:8080/public/xx.js为例说明:

①http://127.0.0.1:8080/,该URL默认一次性指向了4个目录:/META-INF/resources/、/resources/、/static/、/public/;

②http://127.0.0.1:8080/public,则是定位到了/public/目录,然后再去找到xx.js文件;

总结:如果没有指定路径,则Spring Boot默认从/META-INF/resources/、/resources/、/static/、/public/这4个目录依次遍历,直到遍历完毕,去查找我们想要的文件,找到就返回,找不到就返回404;

③通过getWelcomePage方法得知,首页就是静态资源文件夹里面的index.html文件;

Thymeleaf模板引擎

这里,可以通过官网链接来进行阅读:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/spring-boot-features.html#boot-features-webflux-template-engines

 

该模板引擎,主要由2部分组成:

thymeleaf.version

thymeleaf-layout-dialect.version

Thymeleaf语法

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)

public class ThymeleafProperties {

    private static final Charset DEFAULT_ENCODING;

    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".html";

    private boolean checkTemplate = true;

    private boolean checkTemplateLocation = true;

    private String prefix = "classpath:/templates/";

    private String suffix = ".html";

    private String mode = "HTML";

Thymeleaf默认配置,都是以spring.thymeleaf来开头作为属性名,且文件默认存放在类路径下的/templates/目录下,文件默认后缀名为html文件;下面是官网链接:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/appendix-application-properties.html

开发过程就2步:

①导入thymeleaf的名称空间:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

②在HTML文件中使用,至于教程,自己去网上搜

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值