Spring Boot使用thymeleaf模板

Thymeleaf是個XML/XHTML/HTML5模板引擎,可以用於Web與非Web應用。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。可以完全替代JSP。

Thymeleaf 在有網絡和無網絡的環境下皆可運行,即它可以讓美工在瀏覽器查看頁面的靜態效果,也可以讓程序員在服務器查看帶數據的動態頁面效果。這是由於它支持 html 原型,然後在 html 標籤裏增加額外的屬性來達到模板+數據的展示方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,所以 thymeleaf 的模板可以靜態地運行;當有數據返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。

那麼Spring Boot怎樣和thymeleaf整合呢?

首先新建maven項目,導入spring boot的依賴

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.8.RELEASE</version>
</parent>

導入thymeleaf starter pom依賴

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

在src/main/resources下新建static目錄(存放js、css、圖片等靜態資源)和templates目錄(存放展示模板,如html等),將bootstrap相關的js、css放入到static下

個人博客

新建Person類,作爲數據載體

package com.spring.boot.web.model;

public class Person {
	
	private String name;
	
	private int age;
	
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

新建WebController類,指定入口方法,向模板填充數據

package com.spring.boot.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.boot.web.model.Person;

@Controller
public class WebController {
	
	@RequestMapping("/")
	public String index(Model model){
		Person onePerson = new Person("微兒博客", 18);
		
		List<Person> list = new ArrayList<Person>();
		Person p1 = new Person("張三", 18);
		Person p2 = new Person("李四", 19);
		Person p3 = new Person("王五", 20);
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		model.addAttribute("oneperson", onePerson);//向模板傳數據
		model.addAttribute("people", list);
		return "index";//找到名爲index.*的模板
	}
}

在src/main/resources/templates下新建index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>test</title>
<link th:href="@{css/bootstrap.min.css}" rel="stylesheet"/>
</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="${oneperson.name}"></span>
		</div>
	</div>
	<div th:if="${not #lists.isEmpty(people)}">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h3 class="panel-title">列表</h3>
			</div>
			<div class="panel-body">
				<ul class="list-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 type="text/javascript" th:src="@{js/jquery-1.12.3.min.js}"></script>
	<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
	<script th:inline="javascript">
		function getName(name){
			alert(name);
		}
	</script>
</body>
</html>

創建執行類Main

package com.spring.boot.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
	
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

執行,訪問localhost:8080

微兒博客

原文鏈接:http://www.weare.net.cn/article/6e8a585099d2169180abeb04efe8b59f.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章