【发布时间】:2014-01-10 19:02:54
【问题描述】:
我是 Thymeleaf 的新手,并将我的网页从 JSP 转换为 Thymeleaf。我有一个这样的支柱标签:
<c:set var="someVariable" value="${someValue}"/>
该变量可以在 JSP 中的任何地方使用。 Thymeleaf 中是否有这样的替代方案?
【问题讨论】:
标签: java html spring jsp thymeleaf
我是 Thymeleaf 的新手,并将我的网页从 JSP 转换为 Thymeleaf。我有一个这样的支柱标签:
<c:set var="someVariable" value="${someValue}"/>
该变量可以在 JSP 中的任何地方使用。 Thymeleaf 中是否有这样的替代方案?
【问题讨论】:
标签: java html spring jsp thymeleaf
您可以使用local variables。
用th:with 属性声明一个HTML 元素。例如
<div th:with="someVariable=${someValue}">
文档说明
当
th:with被处理时,[someVariable]变量被创建为 局部变量并添加到来自上下文的变量映射中, 以便它与任何其他变量一样可用于评估 从一开始就在上下文中声明,但仅限于边界内 包含标签。
【讨论】:
请注意,如果您希望分配多个变量,请用逗号分隔它们:
<div th:with="someVariable=${someValue},anotherVariable=${anotherValue}">
【讨论】:
用th:with="varName=${'str'}声明
ref with in src th:src="@{${varName}}"
更详细:
<head th:with="component =${'/static/component'}, bizJs = ${'/static/js/biz'}">
<span th:text="${component}"></span>
<script th:src="@{(${component})}"></script>
<script th:src="@{${bizJs} + '/static/js'}"></script>
</head>
【讨论】: