首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >3.语法详解-thymeleaf

3.语法详解-thymeleaf

作者头像
Yuyy
发布2022-06-28 19:44:14
发布2022-06-28 19:44:14
4470
举报

本文最后更新于 909 天前,其中的信息可能已经有所发展或是发生改变。

1. 变量输出

代码语言:javascript
复制
th:text 输出内容
th:value 输出到value属性,例如input标签

2. 字符串操作

  • strings是thymeleaf的内置对象,可以直接使用
  • 调用内置对象要用#
  • 大部分内置对象是以s结尾,例如strings,numbers,dates
代码语言:javascript
复制
${#strings.isEmpty(str)}
${#strings.contains(str1,str2)}
${#strings.startsWith(str1,str2)}
${#strings.endsWith(str1,str2)}
${#strings.length(str)}
${#strings.indexOf(str1,str2)}
${#strings.substring(str,number)}
${#strings.substring(str,number1,number2)}
${#strings.toUpperCase(str)}
${#strings.toLowerCase(str)}

3. 日期格式化处理

代码语言:javascript
复制
${#dates.format(key)} 格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key,'y/M/d')} 按照自定义的格式做日期转换
${#dates.year(key)} 取年
${#dates.month(key)} 取月
${#dates.day(key)} 取日

4. 条件判断

  1. th:if
代码语言:javascript
复制
<span th:if="${sex} = '男'">
性别:男
</span>
<span th:if="${sex} = '女'">
性别:女
</span>
  • 值比较
代码语言:javascript
复制
gt:     great than(大于)>
ge:    great equal(大于等于)>=
eq:    equal(等于)==
lt:    less than(小于)<
le:    less equal(小于等于)<=
ne:    not equal(不等于)!= 
  • 多条件时使用and,or
  1. th:switch
代码语言:javascript
复制
<div th:switch="${id}">
    <span th:case="1>ID 为 1</span>
    <span th:case="2>ID 为 2</span>
    <span th:case="3>ID 为 3</span>
</div>

迭代遍历

  1. th:each
代码语言:javascript
复制
<table border="1">
            <tr>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
            </tr>
            <tr th:each="item:${userList}">
                <td th:text="${item.name}"></td>
                <div th:switch="${item.sex}">
                    <td th:case='1'>男</td>
                    <td th:case='0'>女</td>
                </div>
                <th th:if="${item.age} le 20">青年</th>
                <th th:if="${item.age} gt 20 and ${item.age} le 40">中年</th>
                <th th:if="${item.age} gt 40">老年</th>
            </tr>
        </table>
  • 效果
  • 状态变量 <tr th:each="item,i:${userList}">
代码语言:javascript
复制
<th>index <br> 从0开始的下标</th>
<th>count <br>  从1开始的下标</th>
<th>size <br>  集合大小</th>
<th>even <br>  index是否为偶数</th>
<th>odd <br> index是否为奇数</th>
<th>first <br>  是否为第一个元素</th>
<th>last <br>  是否为最后个元素</th>
代码语言:javascript
复制
<th th:text="${i.index}"></th>
<th th:text="${i.count}"></th>
<th th:text="${i.size}"></th>
<th th:text="${i.even}"></th>
<th th:text="${i.odd}"></th>
<th th:text="${i.first}"></th>
<th th:text="${i.last}"></th>
  • 效果

Map

代码语言:javascript
复制
    @RequestMapping("showMap")
    public String showInfoMap(Model model){
        model.addAttribute("msg", "hello thymeleaf");
        Map<String,User> map=new HashMap<>();
        map.put("id1",new User("张三","男",18));
        map.put("id2",new User("李四","女",32));
        map.put("id3",new User("王五","女",58));
        model.addAttribute("userMap", map);
        return "indexMap";
    }
代码语言:javascript
复制
    <table border="1">
        <tr>
            <th>Id</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr th:each="itemMap:${userMap}">
            <th th:each="temp:${itemMap}" th:text="${temp.key}">
            <th th:each="temp:${itemMap}" th:text="${temp.value.name}">
            <th th:each="temp:${itemMap}" th:text="${temp.value.sex}">
            <th th:each="temp:${itemMap}" th:text="${temp.value.age}">
        </tr>
    </table>
  • 效果

注:遍历map时需要双重循环

Post Views: 332

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 变量输出
  • 2. 字符串操作
  • 3. 日期格式化处理
  • 4. 条件判断
  • 迭代遍历
  • Map
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档