1

1. 模板引擎

前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发会带来非常大的麻烦,那怎么办呢?

SpringBoot推荐你可以来使用模板引擎:

模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想都是一样的,什么样一个思想呢我们来看一下这张图:

Thymeleaf.webp

2. Thymeleaf的引用

怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。给大家三个网址:

Thymeleaf 官网:https://www.thymeleaf.org/

Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf

Spring官方文档:找到我们对应的版本

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/referen...

找到对应的pom依赖:可以适当点进源码看下本来的包!


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

3. Thymeleaf分析

前面呢,我们已经引入了Thymeleaf,那这个要怎么使用呢?

我们首先得按照SpringBoot的自动配置原理看一下我们这个Thymeleaf的自动配置规则,在按照那个规则,我们进行使用。

我们去找一下Thymeleaf的自动配置类:ThymeleafProperties


@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";
    private Charset encoding;
}

我们可以在其中看到默认的前缀和后缀!

我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

也可以在配置文件中指定自己的模板,可以通过yaml实现:

spring:
  thymeleaf:
    prefix: classpath:/templates/
    check-template-location: true
    cache: false
    suffix: .html
    encoding: UTF-8
    content-type: text/html
  • prefix:指定模板所在的目录
  • check-tempate-location: 检查模板路径是否存在
  • cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
  • encoding&content-type:这个大家应该比较熟悉了,与Servlet中设置输出对应属性效果一致

4. Thymeleaf的语法

Thymeleaf 官网:https://www.thymeleaf.org/

4.1表达式

表达式名字语法用途
变量取值${...}获取请求域、session域、对象等值
选择变量*{...}获取上下文对象值
消息#{...}获取国际化等值
链接@{...}生成链接
片段表达式~{...}jsp:include 作用,引入公共页面片段

4.2 字面量

  • 文本值: 'one text' , 'Another one!'
  • 数字: 0 , 34 , 3.0 , 12.3
  • 布尔值: true , false
  • 空值: null
  • 变量: one,two,.... 变量不能有空格

4.3 文本操作

  • 字符串拼接: +
  • 变量替换: |The name is ${name}|

4.4 运算

  • 数学运算符: + , - , * , / , %
  • 布尔运算符:and , or
  • 一元运算:!,not
  • 比较运算:> , < , >= , <= ( gt , lt , ge , le ); 等式: == , != ( eq , ne )

4.5 code

  1. 修改测试请求,增加数据传输:

@RequestMapping("/t1")
public String test1(Model model){
    //存入数据
    model.addAttribute("msg","Hello,Thymeleaf");
    //classpath:/templates/test.html
    return "test";
}
  1. 我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。

我们可以去官方文档的#3中看一下命名空间拿来过来:

 xmlns:th="http://www.thymeleaf.org"
  1. 我们去编写下前端页面:
<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>Hello</title>
</head>
<body>
    <h1 th:text = "${msg}"></h1>
</body>
</html

th

1)th:text:文本替换;

2)th:utext:支持html的文本替换。

3)th:value:属性赋值  

4)th:each:遍历循环元素

5)th:if:判断条件,类似的还有th:unless,th:switch,th:case

6)th:insert:代码块引入,类似的还有th:replace,th:include,常用于公共代码块提取的场景

7)th:fragment:定义代码块,方便被th:insert引用

8)th:object:声明变量,一般和*{}一起配合使用,达到偷懒的效果。

9)th:attr:设置标签属性,多个属性可以用逗号分隔

例:

<!DOCTYPE html><!--名称空间--><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>
    <meta charset="UTF-8">
    <title>Thymeleaf 语法</title></head><body>
    <h2>ITDragon Thymeleaf 语法</h2>
    <!--th:text 设置当前元素的文本内容,常用,优先级不高-->
    <p th:text="${thText}" />
    <p th:utext="${thUText}" />
 
    <!--th:value 设置当前元素的value值,常用,优先级仅比th:text高-->
    <input type="text" th:value="${thValue}" />
 
    <!--th:each 遍历列表,常用,优先级很高,仅此于代码块的插入-->
    <!--th:each 修饰在div上,则div层重复出现,若只想p标签遍历,则修饰在p标签上-->
    <div th:each="message : ${thEach}"> <!-- 遍历整个div-p,不推荐-->
        <p th:text="${message}" />
    </div>
    <div> <!--只遍历p,推荐使用-->
        <p th:text="${message}" th:each="message : ${thEach}" />
    </div>
 
    <!--th:if 条件判断,类似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法-->
    <p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p>
 
    <!--th:insert 把代码块插入当前div中,优先级最高,类似的有th:replace,th:include,~{} :代码块表达式 -->
    <div th:insert="~{grammar/common::thCommon}"></div>
 
    <!--th:object 声明变量,和*{} 一起使用-->
    <div th:object="${thObject}">
        <p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
        <p>TH: <span th:text="*{thName}" /></p><!--${thObject.thName}-->
        <p>DE: <span th:text="*{desc}" /></p><!--${thObject.desc}-->
    </div></body></html>

Controller:

@Controller
public class ThymeleafController {
 
    @RequestMapping("thymeleaf")
    public String thymeleaf(ModelMap map) {
        map.put("thText", "th:text 设置文本内容 <b>加粗</b>");
        map.put("thUText", "th:utext 设置文本内容 <b>加粗</b>");
        map.put("thValue", "thValue 设置当前元素的value值");
        map.put("thEach", Arrays.asList("th:each", "遍历列表"));
        map.put("thIf", "msg is not null");
        map.put("thObject", new ThObject(1L, "th:object", "用来偷懒的th属性"));
        return "grammar/thymeleaf";
    }
}

phang
1 声望3 粉丝