Thymeleaf (詳細)


一、Thymeleaf介紹

Thymeleaf 是一個模版引擎。所謂的模版引擎,就是模版+數據。

1.快速開始

image-20210316091720072

新增的依賴

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

創建頁面到templates文件夾下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

      歡迎你,<span th:text="${name}">貴賓</span>!

</body>
</html>

設計接口返回這個頁面

@Controller
public class MyController {

     @RequestMapping("/show")
    public String show(Model model){
        model.addAttribute("name","xiaoming");
        return "hello";
    }

}

模版引擎的作用:

  • 讓我們寫渲染頁面的時候更加的方便,直接寫html即可。
  • 模版沒有數據,可以直接展示模版中的默認內容,當有數據時展示數據。——頁面不一定必須跑在服務器內,簡化前端頁面的設計步驟。

有數據時:

image-20210316093142441

沒有數據時:

image-20210316093205803

2.字符串

th標簽中要想做字符串連接,可以使用+號或者“||”

   歡迎你,<span th:text="超級vip+${name}">貴賓</span>!
   歡迎你,<span th:text="|超級vip${name}|">貴賓</span>!

3.if-else

   <span th:if="${age > 18}">成年人,可以做很多事情</span>
<br/>
   <span th:unless="${age > 18}">成年人,可以做很多事情</span>

4.三元運算

 <span th:text="${age > 18?'爽!':'累'}">成年人,可以做很多事情</span>

5.循環

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標簽*必須*放在最前面,任何其他內容都*必須*跟隨其后! -->
    <title>學生列表</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是為了讓 IE8 支持 HTML5 元素和媒體查詢(media queries)功能 -->
    <!-- 警告:通過 file:// 協議(就是直接將 html 頁面拖拽到瀏覽器中)訪問頁面時 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<table class="table table-striped">
    <tr>
        <td>序號</td>
        <td>姓名</td>
        <td>年齡</td>
        <td>性別</td>
    </tr>
    <tr th:each="stu : ${stus}">
        <td th:text="${stu.id}">序號</td>
        <td th:text="${stu.name}">姓名</td>
        <td th:text="${stu.age}">年齡</td>
        <td th:text="${stu.gender}">性別</td>
    </tr>
</table>



<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依賴 jQuery,所以必須放在前邊) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據需要只加載單個插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>

后端數據:

@RequestMapping("/students")
    public String students(Model model){

        //1.創建List
        List<Student> stus = new ArrayList<>();
        stus.add(new Student(1001L,"小明",20,"男"));
        stus.add(new Student(1002L,"小謝",21,"男"));
        stus.add(new Student(1003L,"小王",22,"女"));
        //2.傳給前端頁面
        model.addAttribute("stus",stus);

        return "students";
    }

6.日期格式化

<input th:value="${#dates.format(now,'yyyy-MM-dd HH:mm:ss')}"/>

二、Thymeleaf的案例-修改用戶數據

設計前端頁面

image-20210316105153559


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM