SpringBoot学习---006---thymeleaf

1.介绍

官方文档地址:https://www.thymeleaf.org/

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

2.举例使用

(1)引入thymeleaf依赖

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>

(2)html页面引入

<html xmlns:th="http://www.thymeleaf.org">

(3)编写Controller

@Controller
public class HelloController {
    Logger logger=LoggerFactory.getLogger(getClass());

    @RequestMapping("/hello")
    public String hello(Map<String, String> map) {
        map.put("hello","你好");
        logger.info("");
        return "hello";
    }
}

(4)页面获取后端的值

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello today!!!
<div th:text="${hello}"></div>
</body>
</html>

(5)页面展示:

SpringBoot学习---006---thymeleaf

3.语法规则

(1)th:text:改变当前元素文本内容

th:任意html属性:来替换原生属性的值。

SpringBoot学习---006---thymeleaf

(2)表达式

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: _
@Controller
public class HelloController {
    Logger logger=LoggerFactory.getLogger(getClass());

    @RequestMapping("/hello")
    public String hello(Model model) {
        Person person=new Person();
        person.setAge(10);
        person.setName("yyyy");
        model.addAttribute("person",person);
        return "hello";
    }
}
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello today!!!
<div th:text="${person.name}"></div>
</body>
</html>

 

上一篇:4.2. Value Expressions


下一篇:【jsqlpaser使用006】 合并列