springBoot怎么與Thymeleaf模板引擎結(jié)合使用

這篇文章給大家介紹spring Boot怎么與Thymeleaf模板引擎結(jié)合使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

網(wǎng)站制作、網(wǎng)站設(shè)計(jì)服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠(chéng)為客戶提供服務(wù)是我們的理念。創(chuàng)新互聯(lián)把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來(lái)開(kāi)發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!

Thymeleaf:

  1. Thymeleaf是一個(gè)java類(lèi)庫(kù),他是一個(gè)xml/xhtml/html5的模板引擎,可以作為mvc的web應(yīng)用的view層。
  2. Thymeleaf還提供了額外的模塊與Spring MVC集成,所以我們可以使用Thymeleaf完全替代jsp。

spring Boot

  1. 通過(guò)org.springframework.boot.autoconfigure.thymeleaf包對(duì)Thymeleaf進(jìn)行了自動(dòng)配置。
  2. 通過(guò)ThymeleafAutoConfiguration類(lèi)對(duì)集成所需要的bean進(jìn)行自動(dòng)配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。

下面我將演示spring boot 日常工作中常用的Thymeleaf用法。

Spring Boot 日常工作中常用Thymeleaf的用法

1:首先,在創(chuàng)建項(xiàng)目的時(shí)候選擇依賴中選中Thymeleaf,或者在pom中添加依賴

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

或者項(xiàng)目名-右鍵-add Framework Support來(lái)添加依賴jar包。如圖

spring Boot怎么與Thymeleaf模板引擎結(jié)合使用 

spring Boot怎么與Thymeleaf模板引擎結(jié)合使用  

2:示例javaBean

此類(lèi)用來(lái)在模板頁(yè)面展示數(shù)據(jù)用。包含name和age屬性。

public class Person {
  private String name;
  private Integer age;

  public Person(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

3.腳本樣式靜態(tài)文件

根據(jù)默認(rèn)原則,腳本樣式,圖片等靜態(tài)文件應(yīng)放置在src/main/resources/static下,這里引入了Bootstrap和jQuery,結(jié)構(gòu)如圖所示:

spring Boot怎么與Thymeleaf模板引擎結(jié)合使用 

4.演示頁(yè)面

根據(jù)默認(rèn)原則,頁(yè)面應(yīng)放置在src/main/resources/templates下。在src/main/resources/templates下面新建index.html,如上圖。
代碼如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <meta name="viewport" content="width=device-width,initial-scale=1"/>
  <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="external nofollow" rel="stylesheet"/>
  <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="external nofollow" rel="stylesheet"/>
  <meta charset="UTF-8"/>
  <title>Title</title>
</head>
<body>
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h4 class="panel-title">訪問(wèn)model</h4>
    </div>
    <div class="panel-body">
      <span th:text="${singlePerson.name}"></span>
    </div>
    <div th:if="${not #lists.isEmpty(people)}">
      <div class="panel panel-primary">
        <h4 class="panel-title">列表</h4>
      </div>
      <div class="panel-body">
        <ul class="panel-group">
          <li class="list-group-item" th:each="person:${people}">
            <span th:text="${person.name}"></span>
            <span th:text="${person.age}"></span>
            <button class="btn" th:onclick="'getName(\''+${person.name}+'\')'">獲得名字</button>
          </li>
        </ul>
      </div>

    </div>
  </div>
<script th:src="@{jquery-1.10.2.min.js}" type="text/javascript"></script>
<script th:src="@{bootstrap/js/bootstrap.min.js}"></script>
<script th:inline="javascript">
  var single=[[${singlePerson}]];
  console.log(single.name+"/"+single.age);
  function getName(name) {

      console.log(name);

  }
</script>
</body>
</html>

5.數(shù)據(jù)準(zhǔn)備

代碼如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@Controller
@SpringBootApplication
public class ThymeleafTestApplication {
  @RequestMapping("/")
  public String index(Model model){
    Person single=new Person("aa",1);
    List<Person> people=new ArrayList<Person>();
    Person p1=new Person("bb",2);
    Person p2=new Person("cc",3);
    Person p3=new Person("dd",4);
    people.add(p1);
    people.add(p2);
    people.add(p3);
    model.addAttribute("singlePerson",single);
    model.addAttribute("people",people);
    return "index";
  }
  public static void main(String[] args) {
    SpringApplication.run(ThymeleafTestApplication.class, args);
  }


}

6.運(yùn)行

訪問(wèn)http://localhost:8080效果如圖:

spring Boot怎么與Thymeleaf模板引擎結(jié)合使用 

單擊“獲得名字” f12產(chǎn)看頁(yè)面控制臺(tái)打印的日志效果如圖:

spring Boot怎么與Thymeleaf模板引擎結(jié)合使用

關(guān)于spring Boot怎么與Thymeleaf模板引擎結(jié)合使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

名稱欄目:springBoot怎么與Thymeleaf模板引擎結(jié)合使用
當(dāng)前路徑:http://bm7419.com/article32/pssdsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站制作、搜索引擎優(yōu)化品牌網(wǎng)站制作、用戶體驗(yàn)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司