首页 > 技术文章 > Themleaf结合spring boot使用

ngstx 2021-10-18 16:32 原文

Themleaf结合spring boot使用

Themleaf的maven模板引擎

	<!--引入thymeleaf依赖-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <!--<version></version>-->
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <!--<version>3.0.4.RELEASE</version>-->
        </dependency>

创建对应目录结构

image-20211018162656779

Controller

@Controller
public class MyController {

    @RequestMapping("/")
    public String toIndex(Model model){
        model.addAttribute("msg", "hello");
        return "index";
    }
}

对应html文件中插入模板引擎

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

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>
    首页
</h1>
<p th:text="${msg}"></p>
</body>
</html>

推荐阅读