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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值