3.模板引擎Thymeleaf

模板引擎Thymeleaf

①创建新项目,选择thymeleaf模板引擎和devTools开发工具和web依赖

②新建WebController文件和index模板文件

WebController.java

package com.shuju.example.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/web")
public class WebController {

    @RequestMapping(value = "index")
    public String index(ModelMap map) {
        map.put("title", "thymeleaf hello word");
        return "index";
    }

}

src/main/resources/templates/index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<h1  th:text="${title}"></h1>
</body>
</html>

③启动测试,title已进行了解析

其他

手动直接引入依赖

修改pom.xml,添加如依赖

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

资料

Thymeleaf 基本用法总结

1:引入Thymeleaf
  1.1:<html xmlns:th="http://www.thymeleaf.org">
        th:
  1.2:@{}
2:数据的访问
    ${}
    th:text="${Person.name}"
3:循环
    th:each="person:${people}"
4:条件判断;
${not#lists.isEmpty(people)}
5:数据输出
<script th:inline="javascript">
 var single=[[${singlePerson}]]
<script>

springboot(四):thymeleaf使用详解

Last updated

Was this helpful?