溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot整合Thymeleaf視圖的方法是什么

發(fā)布時間:2022-01-20 09:52:34 來源:億速云 閱讀:143 作者:iii 欄目:web開發(fā)

這篇文章主要介紹了SpringBoot整合Thymeleaf視圖的方法是什么的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot整合Thymeleaf視圖的方法是什么文章都會有所收獲,下面我們一起來看看吧。

Thymeleaf視圖介紹

先看下官網(wǎng)的介紹:
==Thymeleaf是適用于Web和獨立環(huán)境的現(xiàn)代服務(wù)器端Java模板引擎。
Thymeleaf的主要目標(biāo)是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板 -HTML可以在瀏覽器中正確顯示,也可以作為靜態(tài)原型工作,從而可以在開發(fā)團隊中加強協(xié)作。
Thymeleaf擁有適用于Spring Framework的模塊,與您喜歡的工具的大量集成以及插入您自己的功能的能力,對于現(xiàn)代HTML5 JVM Web開發(fā)而言,Thymeleaf是理想的選擇。==
在SpringBoot中,SpringBoot對Thymeleaf提供了良好的支持,同時也提供了自動化配置,因此在SpringBoot中使用Thymeleaf非??旖莘奖?。

創(chuàng)建SpringBoot項目

創(chuàng)建方法建議使用IDEA快速創(chuàng)建SpringBoot項目,并選擇web、Thymeleaf依賴
創(chuàng)建完成后,IDEA自動在pom中加入了web和Thymeleaf依賴管理,pom.xml:

<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>

配置Thymeleaf

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

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

可以看出相關(guān)的配置信息是從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創(chuàng)建項目時,已經(jīng)自動生成了該目錄。
我們?nèi)绻枰獙hymeleaf的配置進行更改,可直接在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

編寫Demo

1、新建User和UserController:
User.java:

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("網(wǎng)絡(luò)工程");
        u2.setGrade("2017");
        list.add(u2);
        //視圖模板文件的名字,需在template目錄下創(chuàng)建同名模板文件
        ModelAndView mv = new ModelAndView("users");
        mv.addObject("users",list);
        return mv;
    }
}

2、在模板目錄下新建users.html模板文件,顯示數(shù)據(jù):

<!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>專業(yè)</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>

3、啟動項目,訪問http://localhost:8080/users

關(guān)于“SpringBoot整合Thymeleaf視圖的方法是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot整合Thymeleaf視圖的方法是什么”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI