其他分享
首页 > 其他分享> > SpringBoot学习---006---thymeleaf

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)页面展示:

3.语法规则

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

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

(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>

 

标签:...,SpringBoot,public,---,person,thymeleaf,Expressions,hello
来源: https://blog.csdn.net/qq_41653753/article/details/111183002