开发者社区 问答 正文

如何设置Thymeleaf的属性值?

如何设置Thymeleaf的属性值?

展开
收起
问问小秘 2020-04-29 17:11:06 716 分享 版权
1 条回答
写回答
取消 提交回答
    1. th:attr 任何属性值,语法格式:th:attr="属性名=属性值,[属性名=属性值]" 属性值如果是使用表达式的话:通常有URL表达式@{}和变量表达式${}
      但此标签语法不太优雅
      示例:
      th:attr="action=@{/subscribe}" //当然也可以直接使用th:action
      th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" //可直接使用th:src
      th:attr="value=#{subscribe.submit}"//可直接使用th:value

      设置多个属性在同一时间,有两个特殊的属性可以这样设置: th:alt-title 和 th:lang-xmllang
      th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}"
      2.前置和后置添加属性值
      th:attrappend 和 th:attrprepend
      主要对class和style两个属性
      class="btn" th:attrappend="class=${' ' + cssStyle}"
      转换后:class="btn warning"
      3.还有两个特定的添加属性 th:classappend 和 th:styleappend
      与上面的attrappend功能一样
      class="row" th:classappend="${prodStat.odd}? 'odd'"
      转换后:奇数行class="row odd",偶数行class="row"
    2020-04-29 17:11:26
    赞同 展开评论
问答地址: