3.语法详解-thymeleaf
本文最后更新于 1756 天前,其中的信息可能已经有所发展或是发生改变。

1. 变量输出

th:text 输出内容
th:value 输出到value属性,例如input标签

2. 字符串操作

  • strings是thymeleaf的内置对象,可以直接使用
  • 调用内置对象要用#
  • 大部分内置对象是以s结尾,例如strings,numbers,dates
${#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. 日期格式化处理

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

4. 条件判断

  1. th:if
<span th:if="${sex} = '男'">
性别:男
</span>
<span th:if="${sex} = '女'">
性别:女
</span>
  • 值比较
gt:     great than(大于)>
ge:    great equal(大于等于)>=
eq:    equal(等于)==
lt:    less than(小于)<
le:    less equal(小于等于)<=
ne:    not equal(不等于)!= 
  • 多条件时使用and,or
  1. th:switch
<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
<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>
  • 效果
    image
  • 状态变量
    <tr th:each="item,i:${userList}">
<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>
<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>
  • 效果
    image

Map

    @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";
    }
    <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>
  • 效果
    image

    注:遍历map时需要双重循环
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇