th:text和th之间的区别:Thymeleaf中的值

时间:2017-11-19 06:41:45

标签: thymeleaf

我最近才通过我的一个项目开始使用Thymeleaf。我见过几个例子,其中:text = $ {example}正在某些地方使用:value = $ {example}。

我已经阅读了Thymeleaf文档,但无法找到任何明确引用差异的内容,也没有任何关于SO的问题。

任何帮助都会非常感激!感谢。

2 个答案:

答案 0 :(得分:3)

  • th:value是对html属性value的修改。 对于button,input和option元素,value属性指定元素的初始值
  • th:text用于标签主体修改。

div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code:               <div th:value="${value}"/></div> -->
<br/>Result th:value div:  <div value="sometext"/></div>
        
<!--th code:               <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form> 

<!--th code:               <div th:text="${value}"></div> 
Same as:                   <div>[[${value}]]</div> -->
<br/>Result th:text div:   <div>sometext</div>

以下是不同Thymeleaf attributes features

的文档

答案 1 :(得分:1)

让我们看一个例子:

<input type="radio" name="gender" value="male"> Male<br>

如果我们想在此输入标签的值部分使用百日咳,那么我们将使用

<input type="radio" name="gender" th:value="${someValue}"> Male<br>

如果我们想要动态地从控制器发送(此处是男性)文本,那么我们就会使用

<input type="radio" name="gender" th:text="${someText}""> <br>