在SpringBoot中如何使用模板引擎thymeleaf

2022-08-02,,,,

3.模板引擎

JSP,freemarker、thymeleaf等等

springboot推荐使用的Thymeleaf模板引擎

1.引入thymeleaf:

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

2.使用

@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";
    //只要把html页面放在classpath:/templates/,thymeleaf就能自动渲染

thymeleaf3.0使用文档

  1. 导入thymeleaf的名称空间:
<http:html lang="en" xmlns:th="http://www.thymeleaf.org" http: xmlns:http="http://www.w3.org/1999/xhtml"/> >
  1. 使用thymeleaf语法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" />
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>引擎成功</h1>
<!--    th:text 将div中的文本内容设置为-->
    <div th:text="${hello}">这是显示123</div>
</body>
</html>

3.使用规则

  1. th:text;改变当前元素里面的文本内容;
    th:任意HTML属性来替换原生属性的值
Order Feature Attributes
1 Fragment inclusion(片段包含:jsp:include) th:insert th:replace
2 Fragment iteration(遍历:c:foreach) th:each
3 Conditional evaluation(条件判断 c:if) th:if th:unless th:switch th:case
4 Local variable definition(声明变量c:set) th:object th:with
5 General attribute modification(任意属性修改支持prepend,append) 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
  1. 表达式
Simple expressions: 可以写的表达式语法
    Variable Expressions: ${...} 获取表达式是OGNL
        1.获取对象的属性、调用方法
        2.使用内置的基本对象
            #ctx : the context object. 
            #vars: the context variables. 
            #locale : the context locale. 
            #request : (only in Web Contexts) the HttpServletRequest object. 
            #response : (only in Web Contexts) the HttpServletResponse object. 
            #session : (only in Web Contexts) the HttpSession object. 
            #servletContext : (only in Web Contexts) the ServletContext object.
            举例:
            ${session.foo}
        3.内置的一些工具对象:.......
            
    Selection Variable Expressions: *{...}变量的选择表达式和$功能上是一样的,补充使用:配合th:object使用
    Message Expressions: #{...} 获取国际化内容
    Link URL Expressions: @{...} 定义url
    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: _

本文地址:https://blog.csdn.net/ClearLex/article/details/107371381

《在SpringBoot中如何使用模板引擎thymeleaf.doc》

下载本文的Word格式文档,以方便收藏与打印。