springboot-thymeleaf的使用(1)

標題springboot-thymelead的使用介紹

1)pom文件中引入thymeleaf所依賴的組件

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

2)在application.properties文件中禁止thymeleaf緩存

spring.thymeleaf.cache=false

3)在resources目錄下建立文件夾templates
在templates文件下面建立一個html,hello.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"></meta>
    <title>Hello</title>
</head>
<body>
<h1  th:text="${message}">Hello chenyang!</h1>
</body>
</html>

4)建立一個controller作爲訪問hello.html的入口

@Controller
public class HelloController {

    @RequestMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("message", "http://www.baidu.com");
        return "hello";
    }

}

5)啓動程序,瀏覽器輸入localhost:8080/, 可以在web上看見如下內容:

http://www.baidu.com

6)thymeleaf一些語法規則的介紹
A.文本替換標籤的使用th:text,該標籤惡意替換文本

<p th:text="${userName}">neo</p>
<span th:text="'Hello, ' + ${userName} + '!'"></span>
<br/>
<span th:text="|Hello, ${userName}!|"></span>

B.url標籤的使用

<a  th:href="@{http://www.baidu.com/{type}(type=${type})}">look</a>
<div  th:style="'background:url(' + @{${img}} + ');'">

C.switch標籤的使用

<div th:switch="${sex}">
      <p th:case="'woman'">姑娘</p>
      <p th:case="'man'">爺們</p>
      <p th:case="*">未知性別</p>
</div>

D.if標籤的使用

<a th:if="${flag == 'yes'}"  th:href="@{http://favorites.ren/}"> home </a>
<a th:unless="${flag != 'no'}" th:href="@{http://www.baidu.com/}" >cy</a>

E:list標籤的使用

<table>
        <tr  th:each="user,iterStat : ${users}">
            <td th:text="${user.name}">neo</td>
            <td th:text="${user.age}">6</td>
            <td th:text="${user.pass}">213</td>
            <td th:text="${iterStat.index}">index</td>
        </tr>
</table>

F:eq標籤的使用

<input th:value="${age gt 40 ? '中年':'年輕'}"/>
<a th:if="${flag eq 'yes'}"  th:href="@{http://www.baidu.com/}"> favorites </a>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章