1. 程式人生 > 其它 >Themleaf結合spring boot使用

Themleaf結合spring boot使用

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>

建立對應目錄結構

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>