您的位置:首页 > 编程语言 > Java开发

springboot web静态资源

2020-03-28 20:16 197 查看

静态资源

springboot启动原理告诉我们 所有的启动项都有autoConfiguration,传统的SpringMVC资源文件都在项目初始化之后的web文件中,相关的配置也是在WebMvcAutoConfiguration中。

Springboot是一个自动配置的架构设计,所以找到他所封装的配置类WebMvcAutoConfiguration
如果有自定义的资源处理方式 则不再进行加载springboot默认的:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
//如果你自己手动添加一个资源映射路径,下面自动配置的将不会被加载  if语句看的懂吧继续往下
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
//缓冲控制·
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
//判断是否存在一个映射路径/webjars/**
//addResourceLocations处理逻辑 webjars/资源
//addResourceLocations加载资源的路径classpath:/META-INF/resources/webjars/资源
//提到这里就很明白了吧 启动项目标准的接口:http://localhost:8080/
if (!registry.hasMappingForPattern("/webjars/**")) {
.addResourceLocations("classpath:/META-INF/resources/webjars/")	.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
//静态资源路径  点进去----》》private String staticPathPattern = "/**";
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
//看上面的变量是不是下面的一样addResourceHandler(staticPathPattern)
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
//继续往进点
//private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS
//在点--》private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
//{ "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" };
// 看到这里你就会发现其实/**加载的就是上面我点出来的资源路径,接下来看静态资源处理+1的分析
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}

开始加载webjars的资源

就是使用mavn,就可以用jar包的方式,引入前端静态资源。

优点:

将静态资源版本化,更利于升级和维护。
剥离静态资源,提高编译速度和打包效率。
实现资源共享,有利于统一前端开发。
// 从registry看有没有/webjars 有的话加载webjars里面的源文件。
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));
}


webjars是很大的开源项目,里面包含了很多的静态资源。
我们如果引入webjars的包如jQuery,通过以上源码可以判断我们可以通过地址访问得到该包下里面的js文件。

自己的静态资源

我们自己的静态资源引入

String staticPathPattern = this.mvcProperties.getStaticPathPattern();

// 看看staticPathPattern下面有没有静态资源
if (!registry.hasMappingForPattern(staticPathPattern)) {
// 存在资源 相关路径下的资源添加到Cache里面。
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}

}

我们再看看staticPathPattern里面是什么

发现这个是ResourceProperties 下的一个属性:

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;
// 构造器里面将静态常量CLASSPATH_RESOURCE_LOCATIONS 付给了staticLocations
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();

构造器里面将CLASSPATH_RESOURCE_LOCATIONS的值付给了他,意味着staticPathPattern指向了

“classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, "classpath:/public/"这些地址。也就说我们的静态资源只要放在这个路径下也就会被springboot扫描到,就能正常使用了。

thymeleaf模板引擎加载

springboot的资源目录里有template,这里面没有加载这个模板。我们再看看是怎么加载这个的。
1 . 市面上主流的 Java 模板引擎有:JSP、Velocity、Freemarker、Thymeleaf 。
2. JSP 本质也是模板引擎,Spring Boot 官方推荐使用 “Thymeleaf”模板引擎 ,他使用的是HTMl页面。
3. 模板引擎原理图如下,模板引擎的作用都是将模板(页面)和数据进行整合然后输出显示,区别在于不同的模板使用不同的语法,如 JSP 的JSTL表达式,以及J SP 自己的表达式和语法,
4. Thymeleaf 也有自己的语法
Thymelea:就是一种模板渲染技术,它使用特定的语法对html进行渲染,也就是说我们可以直接使用html做为视图层技术。
模板引擎基本都是一个原理,语法不同而已!!!
在前后端分离不支持的情况下, 就是我们必须学会的!!!
引入依赖

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

引入启动类就相当于加入了下面两个类

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>

既然是springboot项目就可以通过找autocofiguration找到他的启动项

我们找到了ThymeleafAutoConfiguration

@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {

AutoConfigureAfter 这个注解我们分开看,前面是Autoconfigure后面是after,我们可以想到是在加载了后面两个类之后,在进行这个类的加载,这也符合web的要求,即先加载WebMvcAutoConfiguration,在加载我们的引擎模板。意味着我们的引擎模板需要有WebMvcAutoConfiguration作为先决条件,我们知道,引擎模板是将今天网页和数据拼接的一个东西,所以这个自动配置可以通过WebFluxAutoConfiguration得到我们想要传到前端的值。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
qq_28945483 发布了6 篇原创文章 · 获赞 0 · 访问量 63 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: