Spring Boot整合Thymeleaf

Thymeleaf 介绍

Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至是纯文本.
是一个模板引擎,可以为模板做数据渲染,

Thymeleaf的基础使用

添加Thymeleaf启动器依赖

添加后记得更新maven

 <!--添加Thymeleaf启动器依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

编写Controller

@Controller
public class ThymeleafModeController{

    /**
     * 页面跳转方法
     */
    @GetMapping("show")
    public String showTlpage(Model model){
        model.addAttribute("msg","Hello Thymeleaf");
        return "index";
    }
}

创建页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Title</title>
</head>
<body>
    <span th:text="展示栏位"></span>
    <hr/>
    <span th:text="${msg}"></span>
</body>
</html>

Thymeleaf 语法讲解

命名空间:xmlns:th=“http://www.thymeleaf.org”

在这里插入图片描述
标签中的th标记,会提示如图展示,Cannot find declaration to go to,原因是没有在页面中添加th命令的空间的定义.
在html标签中增加命名空间,会去掉异常提示并增加代码提示
在这里插入图片描述

字符串与变量输出操作

th:text

在页面输出值

th:value

可以将一个值放入到input标签的value中

字符串操作

Thymeleaf 提供了一些内置对象,内置对象可以直接在模板中使用.这些对象是以#引用的.

内置对象的语法

引用内置对象需要使用#
大部分内置对象的名称都是以s结尾.如:strings,bumbers,dates

${#strings.isEmpty(key)}

判断字符串是否为空,如果为空返回true,否则返回false

${#strings.contains(msg,'T')}

判断字符串是否包含指定的字串,如果包含返回true,否则返回false

${#strings.startsWith(msg,'a')}

判断当前字符串是否以子串开头,如果是返回true,否则返回false

${#strings.endsWith(msg,'a')}

判断当前字符串是否以子串开头,如果是返回true,否则返回false

${#strings.length(msg)}

返回字符串的长度

${#strings.indexOf(msg,'h')}

查找子串的位置,并返回该子串的下标,如果没有则返回-1

${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}

字符串转大小写

日期格式化处理

${#dates.format(key)}

格式化日期,默认的以浏览器默认语言为格式化标准

${#dates.format(key,'yyyy/MM/dd')}

按照自定义的格式做日期转换

${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}

取日期中年月日的值

条件判断

th:if

条件判断

th:switch/th:case

th:swith/th:case 与java中的switch语句等效,有条件地显示匹配的内容,如果有多个匹配结果只选择第一个显示.
th:case="“表示java中switch的default,即没有case的值为true时则显示th:case=”"的内容

 <drv  th:switch="${id}">
        <span th:case="1">1</span>
        <span th:case="2">2</span>
        <span th:case="3">3</span>
        <span th:case="*">*</span>
    </drv>

迭代遍历

th:each

迭代器,用于循环迭代集合

@GetMapping("show")
    public String showTlpage(Model model){
        model.addAttribute("msg","Hello Thymeleaf");
        model.addAttribute("sex","男");
        List<Users> list = new ArrayList<>();
        list.add(new Users("张三","男","10"));
        list.add(new Users("李四","女","9"));
        list.add(new Users("王五","男","8"));
        model.addAttribute("list",list);
        return "index";
    }
<!--循环遍历-->
    <table border="1" width="50%">
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr th:each="u : ${list}">
            <td th:text="${u.userName}"></td>
            <td th:text="${u.userSex}"></td>
            <td th:text="${u.userAge}"></td>
        </tr>
    </table>

在这里插入图片描述

th:each状态变量

1)index:当前迭代器的索引从0开始
2)count:当前迭代对象的计数,从1开始
3)size:被迭代对象的长度
4)odd/even:布尔值,当前循环是否是偶数/计数 从0开始
5)first:布尔值,当前循环是否是第一条,如果是返回true,否则返回false
6)last:布尔值,当前循环是否是最后一条,如果是则返回true,否则返回false

th:each迭代Map
@GetMapping("show")
    public String showTlpage(Model model){
        Map<String ,Users> map = new HashMap<>();
        map.put("user1",new Users("张三","男","10"));
        map.put("user2",new Users("李四","男","10"));
        model.addAttribute("mapList",map);
        return "index";
    }
}
<table border="1" width="50%">
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr th:each="u : ${mapList}">
            <td th:text="${u.value.userName}"></td>
            <td th:text="${u.value.userSex}"></td>
            <td th:text="${u.value.userAge}"></td>
        </tr>
    </table>

操作域对象

模拟插入数据然后取出
HttpServletRequest
HttpSession
Application

 @GetMapping("show")
    public String showTlpage(Model model, HttpServletRequest request){
        request.setAttribute("req","HttpServletRequest");
        request.getSession().setAttribute("ses","HttpSession");
        request.getSession().getServletContext().setAttribute("app","application");
        return "index";
    }
<body>
<hr/>
    HttpServletRequest:<span th:text="${#request.getAttribute('req')}"></span>
    <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
    <hr/>
    HttpSession:<span th:text="${session.ses}"></span>
    <span th:text="${#httpSession.getAttribute('ses')}"></span>
    <hr/>
    Application:<span th:text="${application.app}"></span>
    <span th:text="${#servletContext.getAttribute('app')}"></span>
</body>

URL表达式

在Thymeleaf 中URL表达式的语法格式为 @{}

url类型

绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径</a>
相对路径

相当于当前项目的根

<a th:href="@{/show}">相对路径</a>
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>

在URL中传递参数

在普通格式的URL中传递参数
<a th:href="@{/show?id=1&name=admin}">在普通格式的URL中传递参数一</a>
<hr/>
<a th:href="@{/show(id=2,name=admin)}">在普通格式的URL中传递参数二</a>
<hr/>
<a th:href="@{'/show?id='+${id}+'&name='+${name}}">在普通格式的URL中传递参数三动态参数传递</a>
<hr/>
<a th:href="@{/show(id=${id},name=${name})}">在普通格式的URL中传递参数四</a>

在restful格式的URL中传递参数

需要注意{}中的字段名称要和()中的字段名称一致

<hr/>
<a th:href="@{/show/{id}(id=1)}">在restful格式的URL中传递参数一</a>
<hr/>
<a th:href="@{/show/{id}/{name}(id=2,name=admin)}">在restful格式的URL中传递参数二</a>
<hr/>
<a th:href="@{/show/{id}(id=2,name=admin)}">在restful格式的URL中传递参数三</a>
<hr/>
<a th:href="@{/show/{id}(id=${id},name=${name})}">在restful格式的URL中传递参数四</a>

Spring Boot 中对Thymeleaf的常见配置

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值