【问题标题】:Difference between th:text and th:value in ThymeleafThymeleaf 中 th:text 和 th:value 之间的区别
【发布时间】:2017-11-19 06:41:45
【问题描述】:

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

我浏览了 Thymeleaf 文档,但找不到任何明确引用差异的内容,也没有任何关于 SO 的问题。

任何帮助将不胜感激!谢谢。

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:
    • th:value是对html属性value的修改。 对于按钮、输入和选项元素,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的文档

    【讨论】:

      【解决方案2】:

      让我们看一个例子:

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

      【讨论】:

      • 这就是我想知道的!所以基本上 text 是显示来自控制器的内容,而 value 是填充模型,对吧?
      【解决方案3】:

      th:name => 这将是您将要传递到另一个页面的值的名称(示例场景)。

      th:value => 这将是您要传递的实际值。它可以从模型中获得,也可以直接从数据库中获得。

      <form th:action="@{confirm-pass-details.html}">
          <button type="submit" th:name="event-id" th:value="${event.get().getEventid()}">Buy Passes</button>
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2017-10-05
        相关资源
        最近更新 更多