欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年07月19日 10:56:07   作者:愛(ài)笑的T_T  
thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來(lái)做前端模版引擎。下面這篇文章主要給大家介紹了關(guān)于spring boot使用thymeleaf模板的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。

前言

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

      1.Thymeleaf 在有網(wǎng)絡(luò)和無(wú)網(wǎng)絡(luò)的環(huán)境下皆可運(yùn)行,即它可以讓美工在瀏覽器查看頁(yè)面的靜態(tài)效果,也可以讓程序員在服務(wù)器查看帶數(shù)據(jù)的動(dòng)態(tài)頁(yè)面效果。這是由于它支持 html 原型,然后在 html 標(biāo)簽里增加額外的屬性來(lái)達(dá)到模板+數(shù)據(jù)的展示方式。瀏覽器解釋 html 時(shí)會(huì)忽略未定義的標(biāo)簽屬性,所以 thymeleaf 的模板可以靜態(tài)地運(yùn)行;當(dāng)有數(shù)據(jù)返回到頁(yè)面時(shí),Thymeleaf 標(biāo)簽會(huì)動(dòng)態(tài)地替換掉靜態(tài)內(nèi)容,使頁(yè)面動(dòng)態(tài)顯示。

      2.Thymeleaf 開(kāi)箱即用的特性。它提供標(biāo)準(zhǔn)和spring標(biāo)準(zhǔn)兩種方言,可以直接套用模板實(shí)現(xiàn)JSTL、 OGNL表達(dá)式效果,避免每天套模板、該jstl、改標(biāo)簽的困擾。同時(shí)開(kāi)發(fā)人員也可以擴(kuò)展和創(chuàng)建自定義的方言。

      3.Thymeleaf 提供spring標(biāo)準(zhǔn)方言和一個(gè)與 SpringMVC 完美集成的可選模塊,可以快速的實(shí)現(xiàn)表單綁定、屬性編輯器、國(guó)際化等功能。

下面這篇文章將給

整體步驟:

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

(2)  如何關(guān)閉thymeleaf緩存

(3)  編寫(xiě)模板文件.html

spring Boot默認(rèn)就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依賴(lài)即可:

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

Thymeleaf緩存在開(kāi)發(fā)過(guò)程中,肯定是不行的,那么就要在開(kāi)發(fā)的時(shí)候把緩存關(guān)閉,只需要在application.properties進(jìn)行配置即可:

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

編寫(xiě)模板文件src/main/resouces/templates/helloHtml.html

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

編寫(xiě)訪(fǎng)問(wèn)路徑(com.kfit.test.web.TemplateController):

package com.kfit.test.web; 
 
import java.util.Map; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
 
 
 
/** 
 
 * 模板測(cè)試. 
 
 * @author Administrator 
 
 * 
 
 */ 
 
@Controller 
 
publicclass TemplateController { 
 /** 
 
 * 返回html模板. 
 
 */ 
 
 @RequestMapping("/helloHtml") 
 public String helloHtml(Map<String,Object> map){ 
 
 map.put("hello","from TemplateController.helloHtml"); 
 return"/helloHtml"; 
 } 
} 

啟動(dòng)應(yīng)用,輸入地址:http://127.0.0.1:8080/helloHtml 會(huì)輸出:

Hello.v.2

from TemplateController.helloHtml

使用freemarker

使用freemarker也很簡(jiǎn)單,

在pom.xml加入freemarker的依賴(lài):

<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-freemarker</artifactId> 
</dependency> 

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

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

/** 
 * 返回html模板. 
 */ 
 
 @RequestMapping("/helloFtl") 
 public String helloFtl(Map<String,Object> map){ 
 map.put("hello","from TemplateController.helloFtl"); 
 return"/helloFtl"; 
 } 

訪(fǎng)問(wèn)地址:http://127.0.0.1:8080/helloFtl

Hello.v.2

from TemplateController.helloFtl

thymeleaf和freemarker是可以共存的。

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

本文記錄一下幾點(diǎn):

一、資源文件的約定目錄結(jié)構(gòu)

二、Maven配置

三、開(kāi)發(fā)時(shí)修改thymeleaf模板自動(dòng)重新加載配置

四、thymeleaf常用基礎(chǔ)知識(shí)點(diǎn)

一、資源文件的約定目錄結(jié)構(gòu)

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

spring-boot項(xiàng)目靜態(tài)文件目錄:/src/java/resources/static

spring-boot項(xiàng)目模板文件目錄:/src/java/resources/templates

spring-boot靜態(tài)首頁(yè)的支持,即index.html放在以下目錄結(jié)構(gòu)會(huì)直接映射到應(yīng)用的根目錄下:

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目錄下。然而這個(gè)目錄并不是首頁(yè)文件的默認(rèn)目錄,所以我們需要手動(dòng)將應(yīng)用根路徑映射到/src/java/resources/templates/index.html下。這個(gè)在spring-mvc的Controller下映射一下就可以了。

@RequestMapping("/") 
 public String index(){ 
 return "index"; 
 } 

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

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

如下配置可以修改試圖跳轉(zhuǎn)的前綴和后綴

spring.thymeleaf.prefix: /templates/ 
spring.thymeleaf.suffix: .html 

更過(guò)有關(guān)thymeleaf中的默認(rèn)這是可以查看org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties這個(gè)類(lèi)的屬性

二、Maven配置

在pom.xml中加入如下依賴(lài)

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

原來(lái)關(guān)于spring-boot-starter-web等的依賴(lài)就可以去掉了,因?yàn)閟pring-boot-starter-thymeleaf是包含這些依賴(lài)的。而關(guān)于jsp的依賴(lài)也可以去掉了,因?yàn)槲覀円呀?jīng)完全拋棄jsp了。

三、開(kāi)發(fā)時(shí)修改thymeleaf模板自動(dòng)重新加載配置

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

# 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常用基礎(chǔ)知識(shí)點(diǎn)

1、在html頁(yè)面中引入thymeleaf命名空間,即<html xmlns:th=http://www.thymeleaf.org></html> ,此時(shí)在html模板文件中動(dòng)態(tài)的屬性使用th:命名空間修飾

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

3、訪(fǎng)問(wèn)spring-mvc中model的屬性,語(yǔ)法格式為“${}”,如${user.id}可以獲取model里的user對(duì)象的id屬性

4、循環(huán),在html的標(biāo)簽中,加入th:each=“value:${list}”形式的屬性,如<span th:each=”user:${users}”></span>可以迭代users的數(shù)據(jù)

5、判斷,在html標(biāo)簽中,加入th:if=”表達(dá)式”可以根據(jù)條件顯示html元素

<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時(shí)間不為空,則顯示時(shí)間

6、時(shí)間的格式化,

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

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

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

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

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

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Java 創(chuàng)建PDF打印小冊(cè)子案例

    Java 創(chuàng)建PDF打印小冊(cè)子案例

    這篇文章主要給大家分享Java 創(chuàng)建PDF打印小冊(cè)子案例,PDF打印小冊(cè)子是指將PDF格式文檔在打印成刊物前需要提前進(jìn)行的頁(yè)面排版,以便在打印后裝訂成冊(cè),下面文章內(nèi)容我們將下面以Java代碼展示如何來(lái)實(shí)現(xiàn),需要的朋友可以參考一下
    2021-10-10
  • Java使用Spring JdbcTemplate向in語(yǔ)句中傳遞參數(shù)的教程詳解

    Java使用Spring JdbcTemplate向in語(yǔ)句中傳遞參數(shù)的教程詳解

    這篇文章主要給大家介紹Java如何使用Spring JdbcTemplate向in語(yǔ)句中傳遞參數(shù),文中有詳細(xì)的流程步驟和代碼示例,需要的朋友可以參考下
    2023-07-07
  • JSON.toJSONString使用異常分析

    JSON.toJSONString使用異常分析

    這篇文章主要為大家介紹了JSON.toJSONString使用異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式

    SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式

    這篇文章主要介紹了SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 解決	Spring RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)問(wèn)題

    解決 Spring RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)問(wèn)題

    本文詳解說(shuō)明了RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)的問(wèn)題及其原由,需要的朋友可以參考下
    2020-02-02
  • SpringBoot實(shí)現(xiàn)郵件發(fā)送的示例代碼

    SpringBoot實(shí)現(xiàn)郵件發(fā)送的示例代碼

    電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下
    2022-04-04
  • java使用Socket實(shí)現(xiàn)文件上傳功能

    java使用Socket實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了java使用Socket實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java split()方法中的特殊符號(hào)舉例詳解

    Java split()方法中的特殊符號(hào)舉例詳解

    Java中的split方法可以將一個(gè)字符串按照指定的分隔符進(jìn)行分割,返回一個(gè)字符串?dāng)?shù)組,這篇文章主要給大家介紹了關(guān)于Java split()方法中的特殊符號(hào)的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • SpringBoot(十)之郵件服務(wù)

    SpringBoot(十)之郵件服務(wù)

    這篇文章給大家介紹了SpringBoot郵件服務(wù)的相關(guān)知識(shí),此文是使用springboot最新版本1.5.3進(jìn)行開(kāi)發(fā)的。本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-05-05
  • 使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能

    使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能

    這篇文章主要介紹了使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能,,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評(píng)論