模板引擎,原来用jsp现在使用thymeleaf

01 导入依赖


使用thyme leaf需要先导入依赖
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#using-boot-starter
https://www.thymeleaf.org/
https://github.com/thymeleaf/thymeleaf
在SpringBoot中找到和thyme leaf相关的依赖
image.png
https://github.com/spring-projects/spring-boot/tree/v2.1.6.RELEASE/spring-boot-project/spring-boot-starters/spring-boot-starter-thymeleaf/pom.xml

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

导入到pom.xml

或者是直接导入

1
2
3
4
<dependency>  
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

idea中使用ctrl+N全局查询

导入后查找thymeleaf.properties类,跳转

1
2
3
4
public class ThymeleafProperties {  
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

约定目录为/templates ,文件后缀为html

测试一下,创建一个html文件名为test。

在此处为什么不说创建一个test.html文件呢?因为thymeleaf会识别到创建html文件但无法直接识别后缀为html的文件,假设直接创建test.html,则没有补全。

templates/test.html文件

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>  
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test</h1>
<!--加上了<h1>标签,假设模板生效则字号为h1,无效则为正文字号-->
</body>
</html>

src\main\java\org\example\demo7\controller\testController.java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package org.example.demo7.controller;  

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;

@Controller
//@RequestMapping("/hello")
public class helloController {
@GetMapping("/test")
// @ResponseBody
// 使用@ResponseBody会直接输出return的内容
public String test() {
return "test";
}
}

访问text路由,发现输出为h1样式,模板渲染成功

02 using


官方使用文档
https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html

按照使用文档的说明 ,导入约束
image.png
有命名空间就可以使用其相关语法了。我们直接去看语法

  • Simple expressions:
    • Variable Expressions: ${...}
    • Selection Variable Expressions: *{...}
    • Message Expressions: #{...}
    • Link URL Expressions: @{...}
    • Fragment Expressions: ~{...}
  • Literals
    • Text literals: 'one text''Another one!',…
    • Number literals: 0343.012.3,…
    • Boolean literals: truefalse
    • Null literal: null
    • Literal tokens: onesometextmain,…
  • Text operations:
    • String concatenation: +
    • Literal substitutions: |The name is ${name}|
  • Arithmetic operations:
    • Binary operators: +-*/%
    • Minus sign (unary operator): -
  • Boolean operations:
    • Binary operators: andor
    • Boolean negation (unary operator): !not
  • Comparisons and equality:
    • Comparators: ><>=<= (gtltgele)
    • Equality operators: ==!= (eqne)
  • Conditional operators:
    • If-then: (if) ? (then)
    • If-then-else: (if) ? (then) : (else)
    • Default: (value) ?: (defaultvalue)
  • Special tokens:
    • No-Operation: _

写个demo

所有的html元素都可以被thymeleaf替换接管: th:html元素

/templates/test.html文件

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>  
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>

src\main\java\org\example\demo7\controller\testController.java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.example.demo7.controller;  
import org.springframework.ui.Model;
//import ch.qos.logback.core.model.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class helloController {
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("msg","springBoot");
return "test";
}
}

注意model中可能存在没有addAttribute方法的问题,注意一下model依赖是否正确。是import org.springframework.ui.Model;

运行一下,成功打印。
image.png

测一下其他

  1. 是否转义
    /templates/test.html文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!DOCTYPE html>  
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <div th:text="${msg}"></div>
    <div th:utext="${msg}"></div>
    </body>
    </html>

    src\main\java\org\example\demo7\controller\testController.java文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package org.example.demo7.controller;  
    import org.springframework.ui.Model;
    //import ch.qos.logback.core.model.Model;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class helloController {
    @RequestMapping("/test")
    public String test(Model model) {
    model.addAttribute("msg","<h1>springBoot</h1>");
    return "test";
    }
    }

    image.png

  2. 数组遍历
    /templates/test.html文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <!DOCTYPE html>  
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <div th:text="${msg}"></div>
    <div th:utext="${msg}"></div>

    <div th:each="user:${users}" th:text="${user}"></div>
    </body>
    </html>

    src\main\java\org\example\demo7\controller\testController.java文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package org.example.demo7.controller;  
    import org.springframework.ui.Model;
    //import ch.qos.logback.core.model.Model;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    import java.util.Arrays;

    @Controller
    public class helloController {
    @RequestMapping("/test")
    public String test(Model model) {
    model.addAttribute("msg","<h1>springBoot</h1>");

    model.addAttribute("users", Arrays.asList("symya1","symya2","symya3"));

    return "test";
    }
    }

等等跟着用就完事了,跟vue很像