1. 程式人生 > 其它 >springboot模板引擎Thymeleaf

springboot模板引擎Thymeleaf

Thymeleaf

  1.匯入依賴

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

  2.檢視原始碼

  

  thymeleaf的html都寫在resources/templates下

  3.測試前端程式碼

  注意新增名稱空間

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

</body>
</html>

  4.後臺請求程式碼

package
com.chen.boot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping("/test") public String test(Model model){ model.addAttribute(
"msg","success"); return "test"; } }

  5.基本語法: