Thymeleaf小记

1.@{}和${}

@{}里放的是超链接,${}里放的是属性值

示例代码

<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- @{/auth} 解析后:/demo/auth -->
<form th:action="@{/auth}" method="post">
    <!-- 传递 method 请求参数,目的是为了让当前请求调用 AuthServlet 中的 login() 方法 -->
    <input type="hidden" name="method" value="login" />

    <!-- th:text 解析表达式后会替换标签体 -->
    <!-- ${attrName} 从请求域获取属性名为 attrName 的属性值 -->
    <p style="color: red;font-weight: bold;" th:text="${message}"></p>
    <p style="color: red;font-weight: bold;" th:text="${systemMessage}"></p>

    账号:<input type="text" name="loginAccount"/><br/>
    密码:<input type="password" name="loginPassword"><br/>
    <button type="submit">进宫</button>
</form>

</body>
</html>

2.th:href="@{/page.do(operate='page',page='cart/cart')}"

超链接需要发送多个请求参数,放在小括号内,逗号隔开

代码示例

<a th:href="@{/page.do(operate='page',page='cart/cart')}" class="cart iconfont icon-gouwuche">

3.使用RESTful的超链接需要拼接参数

方式一:路径用单引号标明,拼接的参数写在@{}中,用+${参数}拼接

代码示例

<a @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">delete</a>

方式二:路径不用单引号标明,拼接的参数不写在@{}中,在它后面拼接

代码示例

<a @click="deleteEmployee" th:href="@{/employee/}+${employee.id}">delete</a>
posted @ 2023-03-28 08:00  飞哥传书  阅读(19)  评论(0)    收藏  举报