thymeleaf语法

参考链接:https://blog.csdn.net/wangmx1993328/category_7804809.html

链接

    <a th:href="@{userList}">1、@{userList}</a><br/>
    <a th:href="@{./userList}">2、@{./userList}</a><br/>
    <a th:href="@{../tiger/home}">3、@{../tiger/home}</a><br/>
    <a th:href="@{/tiger/home}">4、@{/tiger/home}</a><br/>
    <a th:href="@{https://www.baidu.com}">5、@{https://www.baidu.com}</a><br/>

链接参数

    <a th:href="@{userList(id=9527)}">1、@{userList(id=9527)}</a><br/>
    <a th:href="@{userList(id=9527,name=华安)}">2、@{userList(id=9527,name=华安)}</a><br/>
    <a th:href="@{userList(id=9527,name=${userName})}">3、@{userList(id=9527,name=${userName})}</a><br/>

文本

    <!--空格属于特殊字符,必须使用单引号包含整个字符串,不用单引号会报错-->
    <p class="css1 css2" th:class="'css1 css2'">样式</p>
    <!--后台使用:model.addAttribute("info", "Love you 中国"); 传值有空格也是没有问题的-->
    <p th:text="${info}">info</p>
    <!--后台传值字符串有空格是可以的,可以使用 + 进行字符串连接-->
    <p th:text="'small smile'+',very good.'">浅浅的微笑</p>

数字

    <!--数字可以参与运算-->
    <p th:text="${8+8}"></p>
    <p th:text="${number+8}"></p>

布尔

    <!--布尔运算、标签的显示和隐藏-->
    <p th:if="${IsShow} or true">运算结果是false的时候我就不显示了</p>
    <p th:if="${person.age}&gt;10">运算结果是false的时候我就不显示了</p>

NULL

    <p th:text="${IamNull}">IamNull是null的时候,标签内容没有了,实际效果就是看不到标签了</p>
    <p th:text="${IamNull} eq null">判断是否为null</p>
    <p th:text="${IamEmpty} == ''">判断是否为空</p>

算术运算

    <p th:text="15 * 4">值为 60 </p>
    <p th:text="15 * 4-100/10">值为 50 </p>
    <p th:text="100 % 8">值为 4</p>

比较、逻辑运算

    <p th:text="5>4">5 大于 3</p>
    <p th:text="5 &gt;4">5 大于 4</p>
    <p th:text="10>=8 and 7 !=8">10大于等于8,且 7 不等于 8 </p>
    <p th:text="!false">!false</p>
    <p th:text="not(false)">not(false)</p>

三元运算符

    <!--7大于5时,输出 7大,否则输出 5大-->
    <p th:text="7>5?'7大':'5大'">三元运算符</p>
    <!--后台控制器输出了:model.addAttribute("age", 35);-->
    <!--因为 ${xx}取值时,如果值为null,则默认整个标签不再显示-->
    <p th:text="${age}!=null?${age}:'age等于 null'"></p>
    <!--这里使用嵌套判断,嵌套的部分要使用括号-->
    <p th:text="${age}!=null?(${age}>=18?'成年':'未成年'):'age等于 null'"></p>
    <!--变量 age2 后台并没有输出,所以 age2 不存在,此时 age2 ==null-->
    <p th:text="${age2}!=null?${age2}:'age2等于 null'"></p>
    <!--后台输出了:model.addAttribute("isMarry", true);-->
    <!--A>B?X:Y,这里的 Y 部分是省略的,此时如果 A>B 返回 false,则整个三元运算结果为 null-->
    <p th:class="${isMarry}?'css2'">已婚</p>
    <p th:text="!${isMarry}?'css2'">已婚</p>
原文地址:https://www.cnblogs.com/yinchh/p/11919480.html