【问题标题】:setting up a value for a variable name in thymeleaf在 thymeleaf 中为变量名设置一个值
【发布时间】:2014-01-10 19:02:54
【问题描述】:

我是 Thymeleaf 的新手,并将我的网页从 JSP 转换为 Thymeleaf。我有一个这样的支柱标签:

<c:set var="someVariable" value="${someValue}"/>

该变量可以在 JSP 中的任何地方使用。 Thymeleaf 中是否有这样的替代方案?

【问题讨论】:

    标签: java html spring jsp thymeleaf


    【解决方案1】:

    您可以使用local variables

    th:with 属性声明一个HTML 元素。例如

    <div th:with="someVariable=${someValue}">
    

    文档说明

    th:with 被处理时,[someVariable] 变量被创建为 局部变量并添加到来自上下文的变量映射中, 以便它与任何其他变量一样可用于评估 从一开始就在上下文中声明,但仅限于边界内 包含标签。

    【讨论】:

      【解决方案2】:

      请注意,如果您希望分配多个变量,请用逗号分隔它们:

      <div th:with="someVariable=${someValue},anotherVariable=${anotherValue}">
      

      见第三个例子:Local Variable section of Thymeleaf documentation

      【讨论】:

        【解决方案3】:
        1. th:with="varName=${'str'}声明

        2. ref with in src th:src="@{${varName}}"

        3. 更详细:

        <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>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多