一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot使用thymeleaf模板的方法詳解

spring boot使用thymeleaf模板的方法詳解

2020-12-04 08:46愛笑的T_T Java教程

thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來做前端模版引擎。下面這篇文章主要給大家介紹了關于spring boot使用thymeleaf模板的方法,文中通過示例代碼介紹的非常詳細,需要的朋友們下面來一起看看吧。

前言

Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個極吸引人的特點:

      1.Thymeleaf 在有網絡和無網絡的環境下皆可運行,即它可以讓美工在瀏覽器查看頁面的靜態效果,也可以讓程序員在服務器查看帶數據的動態頁面效果。這是由于它支持 html 原型,然后在 html 標簽里增加額外的屬性來達到模板+數據的展示方式。瀏覽器解釋 html 時會忽略未定義的標簽屬性,所以 thymeleaf 的模板可以靜態地運行;當有數據返回到頁面時,Thymeleaf 標簽會動態地替換掉靜態內容,使頁面動態顯示。

      2.Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、 OGNL表達式效果,避免每天套模板、該jstl、改標簽的困擾。同時開發人員也可以擴展和創建自定義的方言。

      3.Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,可以快速的實現表單綁定、屬性編輯器、國際化等功能。

下面這篇文章將給

整體步驟:

(1)  在pom.xml中引入thymeleaf;

(2)  如何關閉thymeleaf緩存

(3)  編寫模板文件.html

spring Boot默認就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依賴即可:

 
?
1
 
2
3
4
5
<dependency>
  <groupId>org.springframework.boot</groupId>
 
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Thymeleaf緩存在開發過程中,肯定是不行的,那么就要在開發的時候把緩存關閉,只需要在application.properties進行配置即可:

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
 
spring.thymeleaf.cache=false

編寫模板文件src/main/resouces/templates/helloHtml.html

 
?
1
 
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 <head>
 <title>Hello World!</title>
 </head>
 <body>
 <h1 th:inline="text">Hello.v.2</h1>
 <p th:text="${hello}"></p>
 </body>
</html>

編寫訪問路徑(com.kfit.test.web.TemplateController):

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.kfit.test.web;
 
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
 
 
/**
 
 * 模板測試.
 
 * @author Administrator
 
 *
 
 */
 
@Controller
 
publicclass TemplateController {
 /**
 
 * 返回html模板.
 
 */
 
 @RequestMapping("/helloHtml")
 public String helloHtml(Map<String,Object> map){
 
 map.put("hello","from TemplateController.helloHtml");
 return"/helloHtml";
 }
}

啟動應用,輸入地址:http://127.0.0.1:8080/helloHtml 會輸出:

 
?
1
 
Hello.v.2

from TemplateController.helloHtml

使用freemarker

使用freemarker也很簡單,

在pom.xml加入freemarker的依賴:

 
?
1
 
2
3
4
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

剩下的編碼部分都是一樣的,說下application.properties文件:

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved

com.kfit.test.web.TemplateController:

 
?
1
 
2
3
4
5
6
7
8
9
/**
 * 返回html模板.
 */
 
 @RequestMapping("/helloFtl")
 public String helloFtl(Map<String,Object> map){
 map.put("hello","from TemplateController.helloFtl");
 return"/helloFtl";
 }

訪問地址:http://127.0.0.1:8080/helloFtl

 
?
1
 
Hello.v.2

from TemplateController.helloFtl

thymeleaf和freemarker是可以共存的。

------------------------------------------------------------------------------------------------------------------------------------------------

本文記錄一下幾點:

一、資源文件的約定目錄結構

二、Maven配置

三、開發時修改thymeleaf模板自動重新加載配置

四、thymeleaf常用基礎知識點

一、資源文件的約定目錄結構

Maven的資源文件目錄:/src/Java/resources

spring-boot項目靜態文件目錄:/src/java/resources/static

spring-boot項目模板文件目錄:/src/java/resources/templates

spring-boot靜態首頁的支持,即index.html放在以下目錄結構會直接映射到應用的根目錄下:

 
?
1
 
2
3
4
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
calsspath:/public/index.html

由于使用thymeleaf的HTML5模板,所以我將index.html模板文件直接放到了/src/java/resources/templates目錄下。然而這個目錄并不是首頁文件的默認目錄,所以我們需要手動將應用根路徑映射到/src/java/resources/templates/index.html下。這個在spring-mvc的Controller下映射一下就可以了。

 
?
1
 
2
3
4
@RequestMapping("/")
 public String index(){
 return "index";
 }

在spring-boot下,默認約定了Controller試圖跳轉中thymeleaf模板文件的的前綴prefix是”classpath:/templates/”,后綴suffix是”.html”

這個在application.properties配置文件中是可以修改的。

如下配置可以修改試圖跳轉的前綴和后綴

 
?
1
 
2
spring.thymeleaf.prefix: /templates/
spring.thymeleaf.suffix: .html

更過有關thymeleaf中的默認這是可以查看org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties這個類的屬性

二、Maven配置

在pom.xml中加入如下依賴

 
?
1
 
2
3
4
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

原來關于spring-boot-starter-web等的依賴就可以去掉了,因為spring-boot-starter-thymeleaf是包含這些依賴的。而關于jsp的依賴也可以去掉了,因為我們已經完全拋棄jsp了。

三、開發時修改thymeleaf模板自動重新加載配置

Spring-boot使用thymeleaf時默認是有緩存的,即你把一個頁面代碼改了不會刷新頁面的效果,你必須重新運行spring-boot的main()方法才能看到頁面更改的效果。我們可以把thymeleaf的緩存關掉,用于支持頁面修改后重新發布到spring-boot內嵌的tomcat中去。在application.properties配置文件中加入以下配置。

 
?
1
 
2
3
4
# Allow Thymeleaf templates to be reloaded at dev time
spring.thymeleaf.cache: false
server.tomcat.access_log_enabled: true
server.tomcat.basedir: target/tomcat

四、thymeleaf常用基礎知識點

1、在html頁面中引入thymeleaf命名空間,即<html xmlns:th=http://www.thymeleaf.org></html> ,此時在html模板文件中動態的屬性使用th:命名空間修飾

2、引用靜態資源文件,比如CSS和JS文件,語法格式為“@{}”,如@{/js/blog/blog.js}會引入/static目錄下的/js/blog/blog.js文件

3、訪問spring-mvc中model的屬性,語法格式為“${}”,如${user.id}可以獲取model里的user對象的id屬性

4、循環,在html的標簽中,加入th:each=“value:${list}”形式的屬性,如<span th:each=”user:${users}”></span>可以迭代users的數據

5、判斷,在html標簽中,加入th:if=”表達式”可以根據條件顯示html元素

 
?
1
 
2
3
<span th:if="${not #lists.isEmpty(blog.publishTime)}">
<span id="publishtime" th:text="${#dates.format(blog.publishTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
</span>

以上代碼表示若blog.publishTime時間不為空,則顯示時間

6、時間的格式化,

 
?
1
 
${#dates.format(blog.publishTime,'yyyy-MM-dd HH:mm:ss')}

表示將時間格式化為”yyyy-MM-dd HH:mm:ss”格式化寫法與Java格式化Date的寫法是一致的。

7、字符串拼接,有兩種形式

比如拼接這樣一個URL:/blog/delete/{blogId}

第一種:th:href="'/blog/delete/' + ${blog.id }" rel="external nofollow"

第二種:th:href="${'/blog/delete/' + blog.id }" rel="external nofollow"

總結

以上就是這篇文章的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://blog.csdn.net/u014695188/article/details/52347318

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 丝袜高跟小说 | 亚洲成色爱我久久 | 色天使亚洲综合在线观看 | 男人的天堂欧美 | 成人高清视频在线观看 | 国产成人精品免费大全 | 欧美va天堂va视频va在线 | 亚州笫一色惰网站 | 精品一区二区三区在线成人 | 视频二区 素人 制服 国产 | 亚洲一区二区三区福利在线 | 成人男女网免费 | 沉香如屑西瓜视频免费观看完整版 | 情人梁家辉在线 | 精品丰满人妻无套内射 | 日本人妖视频 | 精品国产福利一区二区在线 | 天天做天天爱天天爽综合网 | 小小水蜜桃免费影院 | 亚洲AV精品一区二区三区不卡 | 视频在线观看高清免费看 | 国产成人高清亚洲一区91 | 国产成人精选免费视频 | 桥本有菜在线四虎福利网 | 北条麻妃黑人正在播放 | 亚洲区在线播放 | bl文全肉高h湿被灌尿 | 久久久久激情免费观看 | 国产精品永久免费10000 | 91精品国产91热久久p | 欧美人与日本人xx在线视频 | 成人精品网 | 欧美不卡一区二区三区免 | 视频一区国产精戏刘婷30 | 亚洲国产精品热久久 | 日本精品一二三区 | 99视频免费在线 | 国内小情侣一二三区在线视频 | 精品一久久香蕉国产线看播放 | 欧美白虎逼| 湿好紧太硬了我太爽了 |