【问题标题】:Thymeleaf - Loop values into textareaThymeleaf - 将值循环到文本区域
【发布时间】:2020-07-25 21:43:42
【问题描述】:

我在Thymeleaf 中遇到了一个问题,即将值循环到textarea。我的控制器通过模型绑定为我提供了一个列表/集合:

model.addAttribute("response", getDefectMessage.getMessage());

getDefectMessage.getMessage() 返回以下内容(请看下图),在这种情况下我只需要描述。

在我的 HTML 中,我能够显示从模型中检索到的所提供数据的索引和计数(请参阅下面的示例代码)。

<tbody>
    <tr th:each="res, stat : *{response}">
        <td th:text="${stat.count}" />
        <td>
            <textarea type="text" th:text="${res[__${stat.index}__].description}"></textarea>
        </td>
    </tr>
</tbody>

话虽如此,我在渲染时遇到 SpringEL 表达式异常(请参见下图)。

Exception evaluating SpringEL expression: "res[0].description" (defect_details:24)

我是 Thymeleaf 的新手,请问我如何将列表中的值显示到 textarea 中。 (提前致谢)。

【问题讨论】:

  • @Kavithakaran Kanapathippillai,请参阅上面的图片更新。

标签: spring-boot spring-mvc thymeleaf


【解决方案1】:
  • res 代表DefectDto。所以你访问它像res.defectDescription这样的字段

    <tbody>
      <tr th:each="res, stat : *{response}">
          <td th:text="${stat.count}" />
          <td>
              <textarea type="text" th:text="${res.defectDescription}"> 
              </textarea>
          </td>
       </tr>
    </tbody>
  • 或者如果DefectDto有一个方法getDescription()那么你可以这样做
    <tbody>
      <tr th:each="res, stat : *{response}">
          <td th:text="${stat.count}" />
          <td>
              <textarea type="text" th:text="${res.description}"> 
              </textarea>
          </td>
       </tr>
    </tbody>

【讨论】:

  • 愚蠢的我!!!我一定筋疲力尽了。我应该打电话给defectDescription...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2011-08-18
  • 1970-01-01
  • 2012-12-31
  • 2011-11-14
相关资源
最近更新 更多