大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

SpringBoot教程
SpringBoot入門案例
SpringBoot框架Web開發(fā)
SpringBoot非web應(yīng)用程序
SpringBoot使用攔截器
SpringBoot中使用Servlet
SpringBoot中使用Filter
SpringBoot項(xiàng)目配置字符編碼
SpringBoot打包與部署
SpringBoot使用Actuator
SpringBoot集成Thymeleaf模板
SpringBoot總結(jié)及綜合案例
SpringBoot工程下使用Mybatis反向工程

SpringBoot集成Thymeleaf

認(rèn)識(shí)Thymeleaf

Thymeleaf是一個(gè)流行的模板引擎,該模板引擎采用Java語言開發(fā);

模板引擎是一個(gè)技術(shù)名詞,是跨領(lǐng)域跨平臺(tái)的概念,在Java語言體系下有模板引擎,在C#、PHP語言體系下也有模板引擎,甚至在JavaScript中也會(huì)用到模板引擎技術(shù),Java生態(tài)下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(國(guó)產(chǎn)) 等。

Thymeleaf 對(duì)網(wǎng)絡(luò)環(huán)境不存在嚴(yán)格的要求,既能用于Web環(huán)境下,也能用于非Web環(huán)境下。在非Web環(huán)境下,他能直接顯示模板上的靜態(tài)數(shù)據(jù);在Web環(huán)境下,它能像Jsp一樣從后臺(tái)接收數(shù)據(jù)并替換掉模板上的靜態(tài)數(shù)據(jù)。它是基于HTML的,以HTML標(biāo)簽為載體,Thymeleaf要寄托在HTML標(biāo)簽下實(shí)現(xiàn)。

Spring Boot 集成了Thymeleaf模板技術(shù),并且Spring Boot官方也推薦使用Thymeleaf來替代JSP技術(shù),Thymeleaf是另外的一種模板技術(shù),它本身并不屬于Spring Boot,Spring Boot只是很好地集成這種模板技術(shù),作為前端頁面的數(shù)據(jù)展示,在過去的Java Web開發(fā)中,我們往往會(huì)選擇使用Jsp去完成頁面的動(dòng)態(tài)渲染,但是jsp需要翻譯編譯運(yùn)行,效率低。

Thymeleaf的官方網(wǎng)站:http://www.thymeleaf.org

Thymeleaf官方手冊(cè):https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

Spring Boot集成Thymeleaf

項(xiàng)目名稱:039-springboot-thymeleaf-first

1.創(chuàng)建Spring Boot項(xiàng)目,添加web和Thymeleaf依賴

按照這種方式創(chuàng)建后,pom.xml文件下會(huì)自動(dòng)添加如下依賴

<!--SpringBoot集成Thymeleaf的起步依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!--SpringBoot開發(fā)web項(xiàng)目的起步依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.在Spring boot的核心配置文件application.properties中對(duì)Thymeleaf進(jìn)行配置


#thymeleaf頁面的緩存開關(guān),默認(rèn)true開啟緩存
#建議在開發(fā)階段關(guān)閉thymeleaf頁面緩存,目的實(shí)時(shí)看到頁面
spring.thymeleaf.cache=false

其實(shí)什么都不用配置就可以工作,因?yàn)榛綯hymeleaf的配置都有默認(rèn)值

前綴:
#thymeleaf模版前綴,默認(rèn)可以不寫
spring.thymeleaf.prefix=classpath:/templates/



后綴:
#thymeleaf模版后綴,默認(rèn)可以不寫
spring.thymeleaf.suffix=.html

3.創(chuàng)建ThymeleafControlle去映射到模板頁面(和SpringMVC基本一致)

package com.abc.springboot.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:ThymeleafController
 * Package:com.abc.springboot.web
 * Description:

 */

@Controller
public class ThymeleafController {

    @RequestMapping(value = "/springboot/thymeleaf/index")
    public String index(HttpServletRequest request, Model model) {

        model.addAttribute("data","恭喜您,SpringBoot集成Thymeleaf成功");

        return "index";
    }
}

4.在src/main/resources的templates下新建一個(gè)index.html頁面用于展示數(shù)據(jù)

HTML頁面的元素中加入以下屬性:

<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>SpringBoot集成Thymeleaf</title>
</head>
<body >
    <!--Thymeleaf前端框架以Html為載體-->
    <span th:text="${data}"></span>
    <span th:text="${data}"></span>
    <p th:text="${data}"></p>
    <div th:text="${data}"></div>

</body>
</html>

5.啟動(dòng)程序,瀏覽器訪問http://localhost:8080/index

右鍵->查看頁面源代碼

注意:Springboot使用thymeleaf作為視圖展示,約定將模板文件放置在src/main/resource/templates目錄下,靜態(tài)資源放置在src/main/resource/static目錄下

 

全部教程
主站蜘蛛池模板: 国产精品久久久久久免费 | 在线视频一区二区三区四区 | 国产精品全国探花泡良大师 | 国产uv1区二区三区 国产va | 欧美亚洲中日韩中文字幕在线 | 色之综合网 | 男人的天堂久久精品激情 | 美女被a到爽视频在线观看 美女被cao的视频免费看 | 欧美一区二区三区播放 | 亚洲午夜片子大全精品 | 久久夜色精品国产欧美 | 日韩社区 | 在线观看黄色网 | 老太婆性杂交毛片 | 亚洲破处视频 | 99视频观看 | 国产高清美女一级a毛片久久w | 四虎网站最新 | 日本一区中文字幕 | 久草网在线 | 久久久久久国产精品三级 | 久久香蕉国产线看观看精品蕉 | 99热这里只有精品国产免费 | 99精品视频不卡在线观看免费 | 视频一区在线播放 | 久久久国产成人精品 | 亚欧成人毛片一区二区三区四区 | 日本视频播放免费线上观看 | 成人a毛片视频免费看 | 午夜一级成人 | 四虎视频国产精品免费入口 | 日本一级大黄毛片免费基地 | 欧美日韩国产高清一区二区三区 | 国产综合久久久久 | 精品煌色视频网站在线观看 | 国产精品麻豆99久久 | 青青热久免费精品视频精品 | 久久精品这里是免费国产 | 热久久久 | 亚洲一区二区成人 | 99热久久这里只精品国产ww |