SpringBoot-Thymeleaf语法详解

一) 变量输出与字符串操作 针对字符串操作的数据可以根据javaAPI上的方法为参考

3.1.1th:text th:text 在页面中输出值
3.1.2th:value th:value 可以将一个值放入到 input 标签的 value 中
3.1.3 判断字符串是否为空 Thymeleaf 内置对象 注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以 s 结尾 strings、numbers、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.substring(msg,13)}
${#strings.substring(msg,13,15)} 截取子串,用户与 jdk String 类下 SubString 方法相同
${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)} 字符串转大小写。

页面显示

在这里插入图片描述

二)日期类操作展示

操作的对象为dates

<span th:text="${#dates.format(date,'yyyy/MM/dd')}"></span><br/> 格式化 
<span th:text="${#dates.day(date)}"></span><br> 取天
<span th:text="${#dates.year(date)}"></span><br> 取年
<span th:text="${#dates.month(date)}"></span><br/> 取月

三)Thymeleaf条件判断

th:if&th:switch
控制器

@RequestMapping(value = "/show2")
public String show2(Model model){
    model.addAttribute("sex", "男");
    model.addAttribute("id", 1);
    return "index2";
}
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
// th:if条件判断标签
<span th:if="${sex} == '男'">
        性别:男
    </span><br/>

<span th:if="${sex} == '女'">
        性别:女
    </span><br/>
<hr/>
//th:switch 多重选择
<span th:switch="${id}">
        <span th:case="1">ID1</span>
        <span th:case="2">ID2</span>
        <span th:case="3">ID3</span>
    </span><br/>

</body>
</html>

测试效果:
在这里插入图片描述

四)迭代遍历

使用th:each遍历list集合
语法和foreach循环的语法很相似,使用EL获取到集合,每次循环的变量交给临时变量u,从u中获取到数值

@RequestMapping(value = "/show3")
public String show3(Model model){
    List<User> list = Arrays.asList(
            new User("王五","21","男"),
            new User("王五","23","男"),
            new User("王五","24","男")
    );
    model.addAttribute("list", list);
    return "index3";
}
<body>
    <table border="1" width="500px" align="center">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Sex</th>
        </tr>
        <tr th:each="u : ${list}">
            <td th:text="${u.getUserName()}"></td>
            <td th:text="${u.userAge}"></td>
            <td th:text="${u.userSex}"></td>
        </tr>
    </table>

在这里插入图片描述

遍历集合的状态值

index:当前迭代对象的index(从0开始计算)
count: 当前迭代对象的index(从1开始计算)
size:被迭代对象的大小
current:当前迭代变量
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
first:布尔值,当前循环是否是第一个
last:布尔值,当前循环是否是最后一个

<table border="1" width="500px" align="center">
    <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Sex</th>
        <th>Index</th>
        <th>Count</th>
        <th>size</th>
        <th>even</th>
        <th>odd</th>
        <th>first</th>
        <th>last</th>
    </tr>
    <tr th:each="u,var : ${list}">
        <td th:text="${u.getUserName()}"></td>
        <td th:text="${u.userAge}"></td>
        <td th:text="${u.userSex}"></td>
        <td th:text="${var.index}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.even}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.odd}"></td>
        <td th:text="${var.first}"></td>
        <td th:text="${var.last}"></td>
    </tr>
</table>

在这里插入图片描述

使用th:each遍历双列的集合

遍历双列集合的时候需要将使用两次迭代进行遍历,第一次遍历出来的结果是所有的key,通过key获取具体的值,在通过key获取到具体的值

<table border="1" align="center" width="500px">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr th:each = "maps : ${map}">
        <td th:each = "entity : ${maps}" th:text="${entity.value.userName}"></td>
        <td th:each = "entity : ${maps}" th:text="${entity.value.userAge}"></td>
        <td th:each = "entity : ${maps}" th:text="${entity.value.userSex}"></td>
    </tr>
</table>

控制器

model.addAttribute("list", list);
Map<String,User> map = new HashMap<>();
map.put("u1",   new User("王五","21","男"));
map.put("u2",   new User("王五","21","男"));
map.put("u3",   new User("王五","21","男"));
model.addAttribute("map", map);

页面
在这里插入图片描述

五)获取作用域的对象

操作request、session、context

@RequestMapping(value = "/show4")
public String show4(Model model, HttpServletRequest request){
    request.setAttribute("requ", "requ");
    request.getSession().setAttribute("session", "session");
    request.getServletContext().setAttribute("context", "context");
    return  "index4";
}

页面获取

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

request:<span th:text="${#httpServletRequest.getAttribute('requ')}"></span><br/>
session:<span th:text="${session.session}"></span><br/>
application:<span th:text="${application.context}"></span><br/>
<hr>

</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值