如何使用 thymeleaf 将值传递给 bootstrap 的数据?

how to pass a value using thymeleaf to data of bootstrap?

我想使用 Thymeleaf 将值传递给来自 Bootstrap 的数据。

我不知道该怎么做。你能帮帮我吗?

<tr th:each="car : ${lCars}" class="succes">
  <td th:text="${car.id}"></td>
  <td th:text="${car.name}"></td>
  <td align="center">

    <button type="button" class="btn btn-primary"
      data-toggle="modal" data-target="#editar" title="Edit"
      data-id= "th:text="${car.id}""    <!-- I want to pass this value on to the data, but this way doesn't work -->
      data-name="myCar" <!-- If I send a simple word, works well -->

      <span class="glyphicon glyphicon-pencil"></span> Edit
    </button>
  </td>
</tr>

下面这行没有意义: data-id="th:text="${car.id}"""

  1. th:text 的目的是修改元素(在本例中为按钮元素)的正文。您正在尝试设置属性值。
  2. 如果你想让 thymeleaf 修改像 data-id 这样的数据属性,你需要 th:attr 而不是

那一行应该这样写:

data-id="" th:attr="data-id=${car.id}"

如果您希望数据 ID 具有某些默认值,例如 "example-id",请执行以下操作:

data-id="example-id" th:attr="data-id=${car.id}"