thymeleaf

thymeleaf基本操作

添加依赖


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

约束

<html lang="en" xmlns:th="http://www.thymeleaf.org">

字符串和变量的输出

th:text 在页面中输出值

<span th:text="hello"></span>
<hr>
<span th:text="${msg}"></span>

th:value 将一个值放入到input标签的value中

<input th:value="${msg}">

字符串的操作

使用内置对象的语法

1)引用内置对象需要使用#

2)大多数内置对象都以s结尾。如:strings、numbers、dates

${#strings.isEmpty(key)} 判断字符串是否为空,为空返回true

<span th:text="${#strings.isEmpty(msg)}"></span>

${#strings.contains(msg,‘T’)} 判断字符串中是否包含指定的子串,包含返回true

<span th:text="${#strings.contains(msg,'T')}"></span>

${#strings.startsWith(msg,‘a’)} 判断字符串是否以a开头,是返回true

${#strings.endsWith(msg,‘a’)} 判断字符串是否以a结尾,是返回true

${#strings.length(msg)} 返回字符串的长度

${#strings.indexof(msg,‘h’)} 查找子串的位置,返回该字串的下标,没找到返回-1

${#strings.substring(msg,2)}

${#strings.substring(msg,2,5)} 截取子串,与jdk String类下的SubString方法相同

${#strings.toUpperCase(msg)} 转大写

${#strings.toLowerCase(msg)} 转小写

日期格式化处理

#{dates.format(date)} 格式化日期,默认的以浏览器默认语言为格式化处理

<span th:text="${#dates.format(date)}"></span>

${#dates.format(date,‘yyyy/MM/dd’)} 自定义日期格式

<span th:text="${#dates.format(date,'yyyy/MM/dd')}"></span>

${#dates.year(date)} ${#dates.month(date)} ${#dates.day(date)} 分别获取年月日

条件判断

th:if 条件判断

<div>
    <span th:if="${sex}==''">性别:男</span>
    <span th:if="${sex}==''">性别:女</span>
</div>

th:switch/th:case

与java中的switch语句等效,有条件地显示匹配的内容。如果有多个匹配结果只显示第一个

th:case="*" 表示java中的default,即没有case的值为true是则显示th:case="*"的内容

<div th:switch="${id}">
    <span th:case="1">ID为一</span>
    <span th:case="2">ID为二</span>
    <span th:case="*">ID为*</span>
</div>

迭代遍历

th:each 迭代器,用于循环迭代集合

迭代list

<table border="1" width="50%">
    <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>AGE</th>
    </tr>
    <tr th:each="u:${list}">
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
    </tr>
</table>

th:each中的状态变量

1)index:当前迭代器的索引 从0开始

2)count:当前迭代器对象计数从1开始

3)size:被迭代对象的长度

4)odd/even:布尔值,当前循环是否为偶数/奇数 从0开始

5)first:布尔值,当前循环的是否是第一条

6)last:布尔值,当前循环的是否是最后一条

<table border="1" width="50%">
    <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>AGE</th>
        <th>Index</th>
        <th>Count</th>
        <th>Size</th>
        <th>Odd</th>
        <th>Even</th>
        <th>First</th>
        <th>Last</th>
    </tr>
    <tr th:each="u,var:${list}">
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
        <td th:text="${var.index}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.size}"></td>
        <td th:text="${var.odd}"></td>
        <td th:text="${var.even}"></td>
        <td th:text="${var.first}"></td>
        <td th:text="${var.last}"></td>

    </tr>
</table>

运行结果

迭代map

<table border="1" width="50%">
    <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>AGE</th>
        <th>KEY</th>
    </tr>
    <tr th:each="m:${map}">
        <td th:text="${m.value.id}"></td>
        <td th:text="${m.value.name}"></td>
        <td th:text="${m.value.age}"></td>
        <td th:text="${m.key}"></td>
    </tr>
</table>

操作域对象

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>
            <span th:text="${session.get('ses')}"></span>
<hr>
Application:<span th:text="${application.app}"></span>
            <span th:text="${#servletContext.getAttribute('app')}"></span>

url表达式

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

绝对路径
<a th:href="@{http://www.baidu.com}">打开百度</a>
相对路径
相对于当前项目的根
<a th:href="@{/show2}">打开index2</a>
相对于服务器路径的根
<a th:href="@{~/project2/resourcename}"></a>
在URL中传递参数
在普通格式的URL中传递参数
<a th:href="@{/show2?id=1&name=admin}">普通url传递参数方式一</a>
<a th:href="@{/show2(id=2,name=kk)}">普通url传递参数方式二</a>
<a th:href="@{'/show2?id='+${id}+'&name='+${name}}">普通url传递参数方式三</a>
<a th:href="@{/show2(id=${id},name=${name})}">普通url传递参数方式四</a>
在restful格式的URL中传递参数
<a th:href="@{/show3/{id}(id=200)}">restful风格传递参数一</a>
<a th:href="@{/show3/{id}/{name}(id=200,name='ljq')}">restful风格传递参数二</a>
<a th:href="@{/show4/{id}(id=200,name='ljq')}">restful风格传递参数三</a>
<a th:href="@{/show4/{id}(id=${id},name=${name})}">restful风格传递参数四</a>
@GetMapping("/show3/{id}/{name}")
public String show3(@PathVariable String id,@PathVariable String name){
    System.out.println(id+"\t"+name);
    return "index2";
}
@GetMapping("/show4/{id}")
public String show4(@PathVariable String id,String name){
    System.out.println(id+"\t"+name);
    return "index2";
}

在配置文件配置thymeleaf

spring.thymeleaf.prefix=classpath:/templates/suibian/
spring.freemarker.suffix=.html
#配置视图模板类型,如果视图模板为html5需要配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#配置响应类型
spring.thymeleaf.servlet.content-type=text/html 
#配置页面缓存
spring.thymeleaf.cache=false 
erties
spring.thymeleaf.prefix=classpath:/templates/suibian/
spring.freemarker.suffix=.html
#配置视图模板类型,如果视图模板为html5需要配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#配置响应类型
spring.thymeleaf.servlet.content-type=text/html 
#配置页面缓存
spring.thymeleaf.cache=false 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值