1、简单配置
1.1、导入 maven
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2、新建 html
模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test</h1>
<div th:text="${msg}"></div>
</body>
</html>
必须在
html
中配置约束xmlns:th="http://www.thymeleaf.org"
才可以使用thymeleaf
1.3、创建 controller
@RequestMapping("/test")
public String test1(Model model){
model.addAttribute("msg", "this is msg");
return "test";
}
1.4、测试
2、相关标签
2.1、取值方式
- Simple expressions:
- Variable Expressions: ${…}
- Selection Variable Expressions: *{…}
- Message Expressions: #{…}
- Link URL Expressions: @{…}
- Fragment Expressions: ~{…}
- Literals
- Text literals: ‘one text’ , ‘Another one!’ ,…
- Number literals: 0 , 34 , 3.0 , 12.3 ,…
- Boolean literals: true , false
- Null literal: null
- Literal tokens: one , sometext , main ,…
- Text operations:
- String concatenation: +
- Literal substitutions: |The name is ${name}|
- Arithmetic operations:
- Binary operators: + , - , * , / , %
- Minus sign (unary operator): -
- Boolean operations:
- Binary operators: and , or
- Boolean negation (unary operator): ! , not
- Comparisons and equality:
- Comparators: > , < , >= , <= ( gt , lt , ge , le )
- Equality operators: == , != ( eq , ne )
- Conditional operators:
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
- Special tokens:
- No-Operation: _
2.2、标签
Order | Feature | Attributes |
---|---|---|
1 | Fragment inclusion | th:insert th:replace |
2 | Fragment iteration | th:each |
3 | Conditional evaluation | th:if th:unless th:switch th:case |
4 | Local variable definition | th:object th:with |
5 | General attribute modification | th:attr th:attrprepend th:attrappend |
6 | Specific attribute modification | th:value th:href th:src |
7 | Text (tag body modification) | th:text th:utext |
8 | Fragment specification | th:fragment |
9 | Fragment removal | th:remove |