3、SpringBoot整合Thymeleaf視圖

3.1 Thymeleaf視圖介紹

先看下官網的介紹:
==Thymeleaf是適用於Web和獨立環境的現代服務器端Java模板引擎。
Thymeleaf的主要目標是爲您的開發工做流程帶來優雅的天然模板 -HTML能夠在瀏覽器中正確顯示,也能夠做爲靜態原型工做,從而能夠在開發團隊中增強協做。
Thymeleaf擁有適用於Spring Framework的模塊,與您喜歡的工具的大量集成以及插入您本身的功能的能力,對於現代HTML5 JVM Web開發而言,Thymeleaf是理想的選擇。==
在SpringBoot中,SpringBoot對Thymeleaf提供了良好的支持,同時也提供了自動化配置,所以在SpringBoot中使用Thymeleaf很是快捷方便。java

3.2 建立SpringBoot項目

建立方法建議使用IDEA快速建立SpringBoot項目,並選擇web、Thymeleaf依賴:
在這裏插入圖片描述
建立完成後,IDEA自動在pom中加入了web和Thymeleaf依賴管理,pom.xml:web

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

項目架構:
在這裏插入圖片描述spring

3.2 配置Thymeleaf

SpringBoot爲Thymeleaf提供了自動化配置類ThymeleafAutoConfiguration,源碼:後端

@Configuration
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {...}

能夠看出相關的配置信息是從ThymeleafProperties類中得到的,進一步查看ThymeleafProperties的源碼:瀏覽器

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    //省略
}

從該配置能夠看出默認的Thymeleaf存放位置是classpath:/templates/,即resources/templates/下,剛剛咱們使用IDEA建立項目時,已經自動生成了該目錄。
咱們若是須要對Thymeleaf的配置進行更改,可直接在application.properties中配置:緩存

#是否開啓緩存,默認爲true
spring.thymeleaf.cache=false
#檢查模板文件是否存在
spring.thymeleaf.check-template=true
#檢查模本目錄是否存在
spring.thymeleaf.check-template-location=true
#模板文件編碼
spring.thymeleaf.encoding=UTF-8
#模板位置
spring.thymeleaf.prefix=classpath:/templates/
#模板文件後綴名
spring.thymeleaf.suffix=.html
#Content-type
spring.thymeleaf.servlet.content-type=text/html

3.3 編寫Demo

一、新建User和UserController:
User.java:springboot

package com.gongsir.springboot02.pojo;

public class User {
    private String name;
    private String major;
    private String grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

UserController.java:服務器

@Controller
public class UserController {
    @GetMapping(path = "/users")
    public ModelAndView getUsers(){
        List<User> list = new ArrayList<>();
        User u1 = new User();
        u1.setName("龔濤");
        u1.setMajor("計算機");
        u1.setGrade("2017");
        list.add(u1);
        User u2 = new User();
        u2.setName("李詩雅");
        u2.setMajor("網絡工程");
        u2.setGrade("2017");
        list.add(u2);
        //視圖模板文件的名字,需在template目錄下建立同名模板文件
        ModelAndView mv = new ModelAndView("users");
        mv.addObject("users",list);
        return mv;
    }
}

二、在模板目錄下新建users.html模板文件,顯示數據:網絡

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用戶列表</title>
</head>
<body>
    <table border="1px sold black">
        <tr>
            <td>姓名</td>
            <td>專業</td>
            <td>年級</td>
        </tr>
        <tr th:each="user:${users}">
            <td th:text="${user.name}"></td>
            <td th:text="${user.major}"></td>
            <td th:text="${user.grade}"></td>
        </tr>
    </table>
</body>
</html>

三、啓動項目,訪問http://localhost:8080/users,如圖:
在這裏插入圖片描述

3.4 小結

本文主要介紹SpringBoot整合Thymeleaf視圖技術,並給了一個簡單demo演示,想學習更多Thymeleaf知識?看官網吧:https://www.thymeleaf.org/. 不過當前流行先後端分離技術,大多數開發不須要在後端整合視圖技術,後端只須要提供接口便可,待續.....

相關文章
相關標籤/搜索