黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

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

SpringBoot集成Thymeleaf

認識Thymeleaf

Thymeleaf是一個流行的模板引擎,該模板引擎采用Java語言開發;

模板引擎是一個技術名詞,是跨領域跨平臺的概念,在Java語言體系下有模板引擎,在C#、PHP語言體系下也有模板引擎,甚至在JavaScript中也會用到模板引擎技術,Java生態下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(國產) 等。

Thymeleaf 對網絡環境不存在嚴格的要求,既能用于Web環境下,也能用于非Web環境下。在非Web環境下,他能直接顯示模板上的靜態數據;在Web環境下,它能像Jsp一樣從后臺接收數據并替換掉模板上的靜態數據。它是基于HTML的,以HTML標簽為載體,Thymeleaf要寄托在HTML標簽下實現。

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

Thymeleaf的官方網站:http://www.thymeleaf.org

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

Spring Boot集成Thymeleaf

項目名稱:039-springboot-thymeleaf-first

1.創建Spring Boot項目,添加web和Thymeleaf依賴

按照這種方式創建后,pom.xml文件下會自動添加如下依賴

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

<!--SpringBoot開發web項目的起步依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.在Spring boot的核心配置文件application.properties中對Thymeleaf進行配置


#thymeleaf頁面的緩存開關,默認true開啟緩存
#建議在開發階段關閉thymeleaf頁面緩存,目的實時看到頁面
spring.thymeleaf.cache=false

其實什么都不用配置就可以工作,因為基本Thymeleaf的配置都有默認值

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



后綴:
#thymeleaf模版后綴,默認可以不寫
spring.thymeleaf.suffix=.html

3.創建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下新建一個index.html頁面用于展示數據

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.啟動程序,瀏覽器訪問http://localhost:8080/index

右鍵->查看頁面源代碼

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

 

全部教程
主站蜘蛛池模板: 日韩三级免费观看 | 免费看a级 | 国产成人a在一区线观看高清 | 黄网站在线观看视频 | 91桃色视频在线观看 | 亚洲 另类色区 欧美日韩 | 国内日本精品视频在线观看 | 超级碰在线观看 | 国产亚洲欧美日韩综合综合二区 | 在线视频成人 | 亚洲精品中文字幕字幕 | 乡村爱情第16部免费观看 | 午夜免费片在线观看不卡 | 成人国产精品 | 亚洲欧美不卡视频 | 亚洲高清成人欧美动作片 | 深夜免费福利视频 | 欧美一级高清在线观看 | 波多野结衣50连精喷在线 | 成人香蕉视频 | 色网址在线观看 | 色丁香综合 | 欧美巨大另类极品video | 精品福利一区 | 国产免费人成在线看视频 | 老子影院午夜伦不卡不四虎卡 | 一道本香蕉视频 | 日本在线一区二区三区 | 日本在线不卡免费 | 美国一级做a爰片性色毛片 美国一级做a一级视频 | 日韩v| 99视频精品在线 | 亚洲另类自拍丝袜第1页 | 久操视频在线观看 | 色版视频在线观看 | 日韩小片 | 国外性xxxnxxxf视频 | 最近最新2019中文字幕1 | 亚洲视频网址 | 国产精品国产三级国产在线观看 | 一个人在线看的免费视频 |