【问题标题】:Thymeleaf insert text in html code?Thymeleaf 在 html 代码中插入文本?
【发布时间】:2017-03-30 17:24:16
【问题描述】:

我想在 html 代码中插入一个属性。 我试过了,但它不起作用:

<div id="${var}"> ... </div>

我想你知道我的意思。属性“var”应该是 id。我没有找到解决方案...

【问题讨论】:

  • 你试过了吗?

标签: java html css thymeleaf


【解决方案1】:

您只需要使用th:attr 属性。参考文档5.1中有说明:

然后输入 th:attr 属性,以及它改变值的能力 它设置的标签的属性

<form action="subscribe.html" th:attr="action=@{/subscribe}">  
   <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>   
    </fieldset> 
</form> 

概念很简单:th:attr 只接受一个表达式 将值分配给属性。创建了对应的 控制器和消息文件,处理此文件的结果将 是:

<form action="/gtvg/subscribe">   
   <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="¡Suscríbe!"/>   
  </fieldset> 
 </form>

【讨论】:

    【解决方案2】:

    使用这个

    <div th:attr="id=${var}"> ... </div>
    

    【讨论】:

      【解决方案3】:

      Thymeleaf 只评估前缀为 th: 的属性。以下是评估的属性列表:

      http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes

      在您的情况下,th:id 已经内置,因此您只需执行 &lt;div th:id="${var}"&gt; ... &lt;/div&gt; 即可。 th:attr,用于定义thymeleaf 通常不支持的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-14
        • 1970-01-01
        • 1970-01-01
        • 2023-02-04
        • 1970-01-01
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多