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

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

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

服務器之家 - 編程語言 - Java教程 - 詳解spring Boot 集成 Thymeleaf模板引擎實例

詳解spring Boot 集成 Thymeleaf模板引擎實例

2021-01-13 14:36cullinans Java教程

本篇文章主要介紹了spring Boot 集成 Thymeleaf模板引擎實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

今天學習了spring boot 集成thymeleaf模板引擎。發現thymeleaf功能確實很強大。記錄于此,供自己以后使用。

thymeleaf:

  1. thymeleaf是一個java類庫,他是一個xml/xhtml/html5的模板引擎,可以作為mvc的web應用的view層。
  2. thymeleaf還提供了額外的模塊與spring mvc集成,所以我們可以使用thymeleaf完全替代jsp。

spring boot

  1. 通過org.springframework.boot.autoconfigure.thymeleaf包對thymeleaf進行了自動配置。
  2. 通過thymeleafautoconfiguration類對集成所需要的bean進行自動配置。包括templateresolver,templateengine,thymeleafviewresolver的配置。

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

spring boot 日常工作中常用thymeleaf的用法

1:首先,在創建項目的時候選擇依賴中選中thymeleaf,或者在pom中添加依賴

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

或者項目名-右鍵-add framework support來添加依賴jar包。如圖

詳解spring Boot 集成 Thymeleaf模板引擎實例

詳解spring Boot 集成 Thymeleaf模板引擎實例  

2:示例javabean

此類用來在模板頁面展示數據用。包含name和age屬性。

?
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
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.腳本樣式靜態文件

根據默認原則,腳本樣式,圖片等靜態文件應放置在src/main/resources/static下,這里引入了bootstrap和jquery,結構如圖所示:

詳解spring Boot 集成 Thymeleaf模板引擎實例

4.演示頁面

根據默認原則,頁面應放置在src/main/resources/templates下。在src/main/resources/templates下面新建index.html,如上圖。
代碼如下:

?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
<!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">
      <h3 class="panel-title">訪問model</h3>
    </div>
    <div class="panel-body">
      <span th:text="${singleperson.name}"></span>
    </div>
    <div th:if="${not #lists.isempty(people)}">
      <div class="panel panel-primary">
        <h3 class="panel-title">列表</h3>
      </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.數據準備

代碼如下:

?
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
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.運行

訪問http://localhost:8080效果如圖:

詳解spring Boot 集成 Thymeleaf模板引擎實例

單擊“獲得名字” f12產看頁面控制臺打印的日志效果如圖:

詳解spring Boot 集成 Thymeleaf模板引擎實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/haozhuxuan/article/details/53695850

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 糖心在线观看 | 免费网站看v片在线香蕉 | 久久永久免费视频 | 2012中文字幕中字视频 | 亚欧毛片基地国产毛片基地 | 日本无遮挡吸乳视频看看 | 91久久国产露脸精品 | 国产资源视频在线观看 | 亚洲 欧美 国产 综合久久 | 亚洲爱视频 | 久久99精品久久久久久园产越南 | 欧美精品1区| 色花堂中文字幕98堂网址 | 激情影院费观看 | 性欧美xxxxx护士另类 | 欧美在线看片a免费观看 | 日本动漫打扑克动画片樱花动漫 | 千金肉奴隶免费观看 | 成人小视频在线观看 | 911色_911色sss在线观看 | 麻豆视频免费在线观看 | 免费国产成人 | 国产精品久久久久久 | 国产老肥熟xxxx | 国产小视频在线播放 | 国内精品在线观看视频 | 非洲黑人gay巨大 | 国产剧情一区二区三区 | 欧美一区二区三 | 欧美日韩亚洲第一区在线 | 狠狠色狠狠色综合日日小蛇 | 精品视频在线免费观看 | 欧美有码 | 999精品视频这里只有精品 | 精品福利一区二区免费视频 | 女高h | 精品亚洲国产一区二区 | 日韩欧美三级视频 | 日本不卡在线一区二区三区视频 | 亚洲AV无码国产精品午夜久久 | 国产激情一区二区三区四区 |