最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例

 更新時(shí)間:2017年09月30日 09:06:11   作者:cullinans  
本篇文章主要介紹了spring Boot 集成 Thymeleaf模板引擎實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天學(xué)習(xí)了spring boot 集成Thymeleaf模板引擎。發(fā)現(xiàn)Thymeleaf功能確實(shí)很強(qiáng)大。記錄于此,供自己以后使用。

Thymeleaf:

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

spring Boot

  1. 通過org.springframework.boot.autoconfigure.thymeleaf包對(duì)Thymeleaf進(jìn)行了自動(dòng)配置。
  2. 通過ThymeleafAutoConfiguration類對(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來添加依賴jar包。如圖

這里寫圖片描述 

這里寫圖片描述  

2:示例javaBean

此類用來在模板頁面展示數(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)如圖所示:

這里寫圖片描述 

4.演示頁面

根據(jù)默認(rèn)原則,頁面應(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">
      <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.數(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)行

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

這里寫圖片描述 

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

玉田县| 酒泉市| 互助| 华坪县| 扎囊县| 固镇县| 灵川县| 隆化县| 崇义县| 曲水县| 竹北市| 镇坪县| 香河县| 肥东县| 乳山市| 石柱| 金湖县| 建昌县| 合作市| 汪清县| 哈尔滨市| 本溪市| 惠东县| 钦州市| 定南县| 莱西市| 沂水县| 新龙县| 东安县| 沾益县| 弋阳县| 盘锦市| 毕节市| 互助| 龙山县| 东乡县| 安徽省| 潢川县| 乌拉特前旗| 北票市| 荆门市|