1. 程式人生 > >03-SpringBoot之WEB(一)——整合Freemarker與Thymeleaf

03-SpringBoot之WEB(一)——整合Freemarker與Thymeleaf


主要記錄 Spring Boot 與 Web 開發相關的知識,包括整合Freemarker 和 Thymeleaf。

1. 整合 Freemarker

1.1 新增Freemarker依賴

在pom.xml中新增Freemarker依賴,如下:

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

1.2 新增 Freemarker 模板配置

在application.yml中spring節點下新增Freemarker配置,如下:

  #freemarker 配置
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: utf-8
    content-type: text/html
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    expose-request-attributes
: false prefix: suffix: .ftl

1.3 案例開發

新建controller包,在 controller 包中建立 FreemarkerController:

import com.springboot.web.model.User;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import
java.util.ArrayList; import java.util.List; import java.util.Map; @Controller public class FreemarkerController { @RequestMapping("/freemarker") public String hello(Map<String, Object> map) { User u1 = new User(UUIDUtil.getUUID(), "張三", 23); User u2 = new User(UUIDUtil.getUUID(), "李四", 28); User u3 = new User(UUIDUtil.getUUID(), "王五", 25); List<User> uList = new ArrayList<User>(); uList.add(u1); uList.add(u2); uList.add(u3); map.put("uList", uList); map.put("msg", "Hello Freemarker"); return "freemarker"; } }

在 templates 目錄中建立名為 freemarker.ftl 檔案,內容如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div class="container">
    <h2>${msg}</h2>

    <table border="2">
        <tr>
            <td>UUID</td>
            <td>姓名</td>
            <td>年齡</td>
        </tr>

    <#list uList as user>
        <tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
        </tr>
    </#list>
    </table>
</div>

</body>
</html>

執行結果如下:

在這裡插入圖片描述

2. 整合 Thymeleaf

2.1 新增Thymeleaf依賴

在pom.xml中新增Thymeleaf依賴,如下:

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

2.2 新增 thymeleaf 模板配置

在application.yml中spring節點下新增thymeleaf配置,如下:

  #thymeleaf 配置
  thymeleaf:
    cache: true
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    servlet:
      content-type: text/html

2.3 案例開發

新建controller包,在 controller 包中建立 ThymeleafController:

import com.springboot.web.model.User;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ThymeleafController {

    @RequestMapping("/thymeleaf")
    public String hello(Map<String, Object> map) {
        User u1 = new User(UUIDUtil.getUUID(), "張三", 23);
        User u2 = new User(UUIDUtil.getUUID(), "李四", 28);
        User u3 = new User(UUIDUtil.getUUID(), "王五", 25);
        List<User> uList = new ArrayList<User>();
        uList.add(u1);
        uList.add(u2);
        uList.add(u3);
        map.put("uList", uList);
        map.put("msg", "Hello Thymeleaf");
        return "thymeleaf";
    }
}


在 templates 目錄中建立名為 thymeleaf.html檔案,內容如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div class="container">
    <h2 th:text="${msg}"></h2>
    <table border="2">
        <tr>
            <td>UUID</td>
            <td>姓名</td>
            <td>年齡</td>
        </tr>
            <tr th:each="user:${uList}">
                <td th:text="${user.id}"></td>
                <td th:text="${user.name}"></td>
                <td th:text="${user.age}"></td>
            </tr>

    </table>
</div>
</body>
</html>

執行結果如下:

在這裡插入圖片描述

3. 原始碼下載