springboot的web环境
spring官网文档:https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/spring-boot-features.html
本文所有配置,均参考WebMvcAuotConfiguration这个类.这个类也是springbootweb模块的主要配置类.
WebMvcAuotConfiguration
一.springboot对静态资源的默认规则.
1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
引入js依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.1</version>
</dependency>
网页访问
localhost:8080/webjars/jquery/2.1.1/jquery.js
2)"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
- classpath:/META‐INF/resources/ #优先级最高
- classpath:/resources/ #优先级第二
- classpath:/static/ #优先级第三
- classpath:/public/ #优先级最低
说明:虽然这四个包都能访问静态资源,但是优先级有高低之分,说明:优先级指的是如果这几个目录下都有相同的页面,则优先级高的页面被访问到.
3)、欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;
localhost:8080/
#他会去上面四个静态资源包下寻找index.html页面
4)、所有的 **/favicon.ico 都是在静态资源文件下找
logo图片存放位置.
WebMvcAuotConfiguration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
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资源访问路劲
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//静态资源访问路劲
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
//配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping( ResourceProperties resourceProperties) {
return new
WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
二.模板引擎
1.概述
模板引擎就是,构建页面通用模板,在通过注入数据,得到了页面,这就是模板引擎.
1.1 种类
JSP、Velocity、Freemarker、Thymeleaf
1.2 原理
这两个组合起来经过模板引擎加载,就构成了完整的页面了
二.Thymeleaf
具体参考官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf
springboot默认使用Thymeleaf做为模板引擎,并且springboot默认不支持JSP.
1.依赖坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果要自己切换版本最好参考官方文档
2.使用详解
(1) 默认配置
- 编码方式: UTF-8
- 处理的请求类型 text/html
- 默认读取的文件路劲 “classpath:/templates/”;
- 解析出的文件后缀 “.html”
设置默认的配置类是–ThymeleafProperties
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
//默认使用的编码格式
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
//默认读取的页面路劲
public static final String DEFAULT_PREFIX = "classpath:/templates/";
//默认读取的文件的后缀
public static final String DEFAULT_SUFFIX = ".html";
/**
* Whether to check that the template exists before rendering it.
*/
private boolean checkTemplate = true;
(2) 语法提示
<html lang="en" xmlns:th="http://www.thymeleaf.org">
(3) 语法
例如:x=3
-
获取标签及属性(代表上面的x)
th:xx 这个是在具体的页面标签里,操作等- Fragment inclusion (页面片段)
th:insert 插入
th:replace 替换
th:incloud 包含 - Fragment iteration (遍历)
th:each - Conditional evaluation (条件)
th:if
th:unless
th:switch
th:case - Local variable definition
th:object
th:with - General attribute modification(修改属性)
th:attr
th:attrprepend
th:attrappend - Specific attribute modification
- Text (tag body modification)
th:text
th:utext
- Fragment inclusion (页面片段)
-
值表达式(代表上面的3):
Variable Expressions: ${…}
Selection Variable Expressions: *{…}
Message Expressions: #{…}
Link URL Expressions: @{…}
Fragment Expressions: ~{…}
还有很多,参考文档