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

Spring Boot集成Thymeleaf模板引擎的完整步驟

 更新時(shí)間:2019年02月11日 10:11:16   作者:javahih  
這篇文章主要給大家介紹了關(guān)于Spring Boot集成Thymeleaf模板引擎的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

簡(jiǎn)單介紹

目前在JavaEE領(lǐng)域有幾中比較常用的模板引擎,分別是Jsp、Velocity、Freemarker、Thymeleaf,對(duì)Freemark語(yǔ)法不是特別熟悉,不過(guò)對(duì)于前端頁(yè)面渲染效率來(lái)說(shuō),jsp其實(shí)還是最快的,Velocity次之。Thymeleaf雖然渲染效率不是很快,但是語(yǔ)法方面是比較輕巧的,Thymeleaf語(yǔ)法比Velocity輕巧,但是渲染效率不如Velocity

thymeleaf 支持html5標(biāo)準(zhǔn);是一種模板引擎框架(TemplateEngine Framework);thymeleaf 頁(yè)面無(wú)須部署到servlet開(kāi)發(fā)到服務(wù)器上,直接通過(guò)瀏覽器就能打開(kāi)。它可以完全替代 JSP 。

特點(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.它提供標(biāo)準(zhǔn)和spring標(biāo)準(zhǔn)兩種方言,可以直接套用模板實(shí)現(xiàn)JSTL、 OGNL表達(dá)式效果。

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

maven配置

因?yàn)橐肓薙pringBoot的parent工程,所以不需要寫版本號(hào)

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

application.yml配置

#添加Thymeleaf配置
 thymeleaf:
 cache: false
 prefix: classpath:/templates/
 suffix: .html
 mode: HTML5
 encoding: UTF-8
 content-type: text/html

application.yml:

server:
 port: 8081
#logging:
# config: classpath:logback_spring.xml.bat
# level:
# com.muses.taoshop: debug
# path: /data/logs

spring:
 datasource:

 # 主數(shù)據(jù)源
 shop:
  url: jdbc:mysql://127.0.0.1:3306/taoshop?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
  username: root
  password: root

 driver-class-name: com.mysql.jdbc.Driver
 type: com.alibaba.druid.pool.DruidDataSource

 # 連接池設(shè)置
 druid:
  initial-size: 5
  min-idle: 5
  max-active: 20
  # 配置獲取連接等待超時(shí)的時(shí)間
  max-wait: 60000
  # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
  time-between-eviction-runs-millis: 60000
  # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
  min-evictable-idle-time-millis: 300000
  # Oracle請(qǐng)使用select 1 from dual
  validation-query: SELECT 'x'
  test-while-idle: true
  test-on-borrow: false
  test-on-return: false
  # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小
  pool-prepared-statements: true
  max-pool-prepared-statement-per-connection-size: 20
  # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻
  filters: stat,wall,slf4j
  # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄
  connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
  use-global-data-source-stat: true

# jpa:
# database: mysql
# hibernate:
#  show_sql: true
#  format_sql: true
#  ddl-auto: none
#  naming:
#  physical-strategy: org.hibernate.boot.entity.naming.PhysicalNamingStrategyStandardImpl

# mvc:
# view:
#  prefix: /WEB-INF/jsp/
#  suffix: .jsp

 #添加Thymeleaf配置
 thymeleaf:
 cache: false
 prefix: classpath:/templates/
 suffix: .html
 mode: HTML5
 encoding: UTF-8
 content-type: text/html

 #Jedis配置
# jedis :
# pool :
#  host : 127.0.0.1
#  port : 6379
#  password : redispassword
#  timeout : 0
#  config :
#  maxTotal : 100
#  maxIdle : 10
#  maxWaitMillis : 100000

添加html文件

在resources資源文件夾下面新建一個(gè)templates文件夾,所有的html文件都丟在這里,靜態(tài)資源文件也丟在resources資源文件夾下面

新建一個(gè)html文件,然后注意加上<html xmlns:th="http://www.thymeleaf.org">

注意Thymeleaf語(yǔ)法要求比較嚴(yán)格 <meta charset="utf-8" > ,不如這樣寫是不可以的,必須加上斜杠的, <meta charset="utf-8" />

Thymeleaf簡(jiǎn)單例子

遍歷后臺(tái)數(shù)據(jù)

<!--最新上架-->
  <div class="first-pannel clearfix">
   <div class="index-f clearfix">
    <h3 class="index-f-head"> 最新上架 <span>每天都有上新,每天都有驚喜</span> </h3>
    <div class="index-f-body">
     <div class="top-sales newProduct">
      <ul class="top-sales-list clearfix">
       <li class="top-sales-item newProduct" th:each="item : ${items}">
        <p class="item-img"> <a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img th:src="@{${item.imgPath}}" /></a> </p>
        <p class="item-buss"><a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a></p>
        <p class="item-name spec"><a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:text="${item.itemName}"></a></p>
        <p class="item-price spec"><em th:text="${item.mPrice}"></em>元</p>
       </li>
      </ul>
     </div>
    </div>
   </div>
  </div>
  <!--最新上架//-->

引入文件

Thymeleaf引入另外一個(gè)html文件可以使用th:replace或者th:include,

<!--topBar-->
 <div class="topBar" th:replace="/top_bar :: .topBar"></div>
 <!--//topBar-->
 <!--headerMain-->
 <div class="headerMain layout clearfix" th:replace="/header_main :: .headerMain"></div>
 <!--//headerMain-->
 <!--headerNav-->
 
 <div class="headerNav" th:replace="/index_header_nav :: .headerNav"></div>
 <!--//headerNav-->

Img便簽src

 <img th:src="@{/static/images/rachange_ad.jpg}" />

鏈接<a>便簽

靜態(tài)資源使用

<link th:href="@{/static/css/public.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
 <link th:href="@{/static/css/index.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
 <script type="text/javascript" th:src="@{/static/js/jquery-1.3.2.min.js}"></script>
 <script type="text/javascript" th:src="@{/static/js/html5.js}"></script>
 <script type="text/javascript" th:src="@{/static/js/popbox.js}"></script>

給出一個(gè)html頁(yè)面例子:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="utf-8" />
 <title>電商門戶網(wǎng)站</title>
 <link th:href="@{/static/css/public.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
 <link th:href="@{/static/css/index.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
 <script type="text/javascript" th:src="@{/static/js/jquery-1.3.2.min.js}"></script>
 <script type="text/javascript" th:src="@{/static/js/html5.js}"></script>
 <script type="text/javascript" th:src="@{/static/js/popbox.js}"></script>
 <style type="text/css">
  .item-list { display: none; position: absolute; width: 705px; min-height: 200px; _height: 200px; background: #FFF; left: 198px; box-shadow: 0px 0px 10px #DDDDDD; border: 1px solid #DDD; top: 3px; z-index: 1000000; }
  /* Remove Float */
  .clear { display: block; height: 0; overflow: hidden; clear: both; }
  .clearfix:after { content: '\20'; display: block; height: 0; clear: both; }
  .clearfix { *zoom:1;
  }
  .hover { position: relative; z-index: 1000000000; background: #FFF; border-color: #DDD; border-width: 1px 0px; border-style: solid; }
 </style>

</head>
<body>
<!--header-->
<header class="header">
 <!--topBar-->
 <div class="topBar" th:replace="/top_bar :: .topBar"></div>
 <!--//topBar-->
 <!--headerMain-->
 <div class="headerMain layout clearfix" th:replace="/header_main :: .headerMain"></div>
 <!--//headerMain-->
 <!--headerNav-->
 
 <div class="headerNav" th:replace="/index_header_nav :: .headerNav"></div>
 <!--//headerNav-->
</header>
<!--//header-->
<!--container-->
<div class="container">
 <div class="layout clearfix">
  <!--banner-->
  <div class="banner">
   <div class="banner-img">
    <ul>
     <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img width="727" height="350" th:src="@{/static/images/banner_970x355.jpg}" /></a></li>
     <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img width="727" height="350" th:src="@{/static/images/banner_970x355.jpg}" /></a></li>
     <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img width="727" height="350" th:src="@{/static/images/banner_970x355.jpg}" /></a></li>
    </ul>
   </div>
   <ul class="banner-btn">
    <li class="current"></li>
    <li></li>
    <li></li>
   </ul>
  </div>
  <!--//banner-->
  <!--快捷充值-->
  <div class="index-fast-recharge">
   <div class="recharge-body">
    <div class="recharge-head">
     <h2><em class="icon-phone"></em>話費(fèi)充值</h2>
    </div>
    <div class="recharge-con">
     <div class="recharge-form">
      <p>
       <label class="name">手機(jī)號(hào):</label>
       <input placeholder="支持電信" type="text" class="text-box" />
      </p>
      <p>
       <label class="name">充值方式:</label>
       <label>
        <input type="radio" class="rd" />
        電信充值卡</label>
       <label>
        <input type="radio" class="rd" />
        銀行卡</label>
      </p>
      <div class="recharge-sub-btn"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-red">立即充值</a> </div>
     </div>
     <div class="recharge-ad"> <img th:src="@{/static/images/rachange_ad.jpg}" /> </div>
    </div>
   </div>
  </div>
  <!--//快捷充值-->
  <div class="clearfix"></div>
  <!--最新上架-->
  <div class="first-pannel clearfix">
   <div class="index-f clearfix">
    <h3 class="index-f-head"> 最新上架 <span>每天都有上新,每天都有驚喜</span> </h3>
    <div class="index-f-body">
     <div class="top-sales newProduct">
      <ul class="top-sales-list clearfix">
       <li class="top-sales-item newProduct" th:each="item : ${items}">
        <p class="item-img"> <a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img th:src="@{${item.imgPath}}" /></a> </p>
        <p class="item-buss"><a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a></p>
        <p class="item-name spec"><a th:href="@{'/portal/item/toDetail/'+${item.spuId}+'/'+${item.skuId}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" th:text="${item.itemName}"></a></p>
        <p class="item-price spec"><em th:text="${item.mPrice}"></em>元</p>
       </li>
      </ul>
     </div>
    </div>
   </div>
  </div>
  <!--最新上架//-->
 </div>
</div>
<!--//container-->
<!--footer-->
<footer class="footer" th:replace="footer :: .footer"></footer>
<!--//footer-->

<script type="text/javascript">
 //banner
 $(document).ready(function(){
  var demo = $(".banner");
  var btn = $(".banner-btn li");
  var slide = $(".banner-img ul");
  var slideItem = slide.find("li");
  var c = 0, speed = 4000 , t;
  btn.each(function(number){
   $(this).click(function(){
    $(this).addClass("current").siblings().removeClass("current");
    slide.animate({"left":-slideItem.width()*number},300);
    c = number;
   });
  });

  if(btn.size()>1){
   autoSlide();
  }

  function timedCount()
  {
   c = c + 1;
   if(c>=btn.size())c = 0;
   btn.eq(c).click();
  }

  function autoSlide(){
   t = setInterval(function(){timedCount();},speed);
  }
  //鼠標(biāo)移入停止播放
  demo.mouseover(function(){
   clearInterval(t);
  });
  demo.mouseout(function(){
   autoSlide();
  });
 });
</script>
</body>
</html>

代碼取自個(gè)人的開(kāi)源項(xiàng)目:https://github.com/u014427391/taoshop,有需要可以參考

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • java集合類arraylist循環(huán)中刪除特定元素的方法

    java集合類arraylist循環(huán)中刪除特定元素的方法

    下面小編就為大家?guī)?lái)一篇Java集合類ArrayList循環(huán)中刪除特定元素的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼

    springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼

    這篇文章主要介紹了springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼,在為服務(wù)架構(gòu)體系里,網(wǎng)關(guān)是非常重要的環(huán)節(jié),他實(shí)現(xiàn)了很多功能,具體哪些功能大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧
    2018-10-10
  • Java面試題沖刺第二十九天--JVM3

    Java面試題沖刺第二十九天--JVM3

    這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于JVM的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Java根據(jù)key獲取枚舉值的操作方法

    Java根據(jù)key獲取枚舉值的操作方法

    枚舉(enum)算一種“語(yǔ)法糖”,是指一個(gè)經(jīng)過(guò)排序的、被打包成一個(gè)單一實(shí)體的項(xiàng)列表,一個(gè)枚舉的實(shí)例可以使用枚舉項(xiàng)列表中任意單一項(xiàng)的值,本文給大家介紹了Java?如何快速根據(jù)?key?獲取枚舉的值,需要的朋友可以參考下
    2024-07-07
  • Java中自己如何實(shí)現(xiàn)log2(N)

    Java中自己如何實(shí)現(xiàn)log2(N)

    這篇文章主要介紹了Java中自己實(shí)現(xiàn)log2(N)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring在多線程環(huán)境下如何確保事務(wù)一致性問(wèn)題詳解

    Spring在多線程環(huán)境下如何確保事務(wù)一致性問(wèn)題詳解

    這篇文章主要介紹了Spring在多線程環(huán)境下如何確保事務(wù)一致性問(wèn)題詳解,說(shuō)到異步執(zhí)行,很多小伙伴首先想到Spring中提供的@Async注解,但是Spring提供的異步執(zhí)行任務(wù)能力并不足以解決我們當(dāng)前的需求,需要的朋友可以參考下
    2023-11-11
  • Java集合 LinkedList的原理及使用詳解

    Java集合 LinkedList的原理及使用詳解

    這篇文章主要介紹了Java集合 LinkedList的原理及使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java 超詳細(xì)講解抽象類與接口的使用

    Java 超詳細(xì)講解抽象類與接口的使用

    對(duì)于面向?qū)ο缶幊虂?lái)說(shuō),抽象是它的一大特征之一,在 Java 中可以通過(guò)兩種形式來(lái)體現(xiàn)OOP的抽象:接口和抽象類,下面這篇文章主要給大家介紹了關(guān)于Java入門基礎(chǔ)之抽象類與接口的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java8新特性之默認(rèn)方法和靜態(tài)方法

    Java8新特性之默認(rèn)方法和靜態(tài)方法

    這篇文章主要給大家介紹了關(guān)于Java8新特性之默認(rèn)方法和靜態(tài)方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C# 中Excel導(dǎo)入時(shí)判斷是否被占用三種方法

    C# 中Excel導(dǎo)入時(shí)判斷是否被占用三種方法

    這篇文章主要介紹了C# 中Excel導(dǎo)入時(shí) 判斷是否被占用三種方法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評(píng)論