Spring Boot Thymeleaf

一、Thymeleaf 语法讲解

命名空间:xmlns:th="http://www.w3.org/1999/xhtml"

1、字符串与变量输出操作

th:text
在页面中输出值

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

2、字符串操作

Thymeleaf 提供了一些内置对象,内置对象可直接在模板中使用。

使用内置对象的语法

  • 引用内置对象需要使用#
  • 大部分内置对象的名称都以s结尾。如:strings、numbers、dates。

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

${#strings.contains(msg, 'T')}
判断字符串是否包含指定的子串,如果包含返回true,否则返回false。

${#strings.startwith(msg, 'a')}
判断当前字符串是否以子串开头,如果是返回true,否则返回false。

${#strings.endswith(msg, 'a')}
判断当前字符串是否以子串结尾,如果是返回true,否则返回false。

${#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)}
字符串转大小写

3、日期格式化

${#dates.format(key)}
格式化日期,默认的以浏览器默认语言为格式化标准

${#dates.format(key, 'yyyy/MM/dd')}
按照自定义的格式做日期转换

${#dates.year(key)}
取年

${#dates.month(key)}
取月

${#dates.day(key)}
取日

4、条件判断

th:if
条件判断

<!-- 根据性别仅显示一个符合条件的span -->
<div>
    <span th:if="${sex} == '男'">性别:男</span>
    <span th:if="${sex} == '女'">性别:女</span>
</div>

th:switch / th:case
有条件地显示匹配的内容,如果有多个匹配结果只选择第一个显示。th:case="*"表示switch中的default。

<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>
    <span th:case="*">ID不为1、2、3中的任一值</span>
</div>

5、迭代遍历

th:each
迭代器,用于循环迭代集合。th:each迭代可使用两个参数,分别表示迭代对象和迭代状态。可同时使用,也可只使用一个迭代对象。

  1. index:当前迭代器的索引,从0开始。
  2. count:当前迭代器对象的计数,从1开始。
  3. size:被迭代对象的长度
  4. odd/even:布尔值,当前循环是否是偶数/奇数,从0开始。
  5. first:布尔值,当前循环的是否是第一条,如果是返回true则返回false。
  6. last:布尔值,当前循环的是否是最后一条,
<!-- 迭代 list或set -->
<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,s: ${list}">
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
        <td th:text="${s.index}"></td>
        <td th:text="${s.count}"></td>
        <td th:text="${s.size}"></td>
        <td th:text="${s.odd}"></td>
        <td th:text="${s.even}"></td>
        <td th:text="${s.first}"></td>
        <td th:text="${s.last}"></td>
    </tr>
</table>
<!-- 迭代 map -->
<table border="1" width="20%">
    <tr>
        <th>Key</th>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr th:each="m : ${map}">
        <td th:text="${m.key}"></td>
        <td th:text="${m.value.id}"></td>
        <td th:text="${m.value.name}"></td>
        <td th:text="${m.value.age}"></td>
    </tr>
</table>

6、操作域对象

${#request}

<!-- 方式一 -->
<span th:text="${#request.getAttribute('req')}"></span>

<!-- 方式二 -->
<span th:text="${#httpServletRequest.getAttribute('req')}"></span>

${session}

<!-- 方式一 -->
<span th:text="${session.ses}"></span>

<!-- 方式二 -->
<span th:text="${#httpSession.getAttribute('ses')}"></span>

${application}

<!-- 方式一 -->
<span th:text="${application.app}"></span>

<!-- 方式二 -->
<span th:text="${#servletContext.getAttribute('app')}"></span>

7、URL表达式

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

th:href

<!-- 绝对路径 -->
<a th:href="@{http://www.baidu.com}">绝对路径</a>

<!-- 相对于当前项目的根 -->
<a th:href="@{/show}">相对路径</a>

<!-- 相对于服务器的根 -->
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>

二、常见问题

1、如何处理“favicon.ico”问题

资源文件结构:

<!--图标默认为16x16,大于此尺寸也可以-->
+ resources
    - static
        - favicon.ico
        - spring.png

html添加图标:

<!--方式一:使用icon图标-->
<link rel="icon" href="favicon.ico" th:href="@{favicon.ico}">

<!--方式二:使用.png图片-->
<link rel="icon" href="spring.png" th:href="@{spring.png}">

参考资料

1、关于shortcut icon和icon代码的区别介绍

评论

暂无评论