【问题标题】:Unable to iterate over List<List<String>> using Thymeleaf / Spring-Boot无法使用 Thymeleaf / Spring-Boot 遍历 List<List<String>>
【发布时间】:2017-03-15 23:20:54
【问题描述】:

使用 Thymeleaf,我无法遍历 List>

 <tr th:each="row : ${rows}">
      <td th:text="${row[0]}"></td>
      <td th:text="${row[1]}"></td>
 </tr>

每一行有多个值。

渲染时出现以下异常

2016-11-02 20:40:21.033 ERROR 44829 --- [nio-5252-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-5252-exec-1] Exception processing template "completed": Exception evaluating SpringEL expression: "row[1]" (completed:217)
2016-11-02 20:40:21.034 ERROR 44829 --- [nio-5252-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/test-dashboard] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "row[1]" (completed:217)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1025E:(pos 3): The collection has '1' elements, index '1' is invalid

提前致谢。

【问题讨论】:

  • 我不是百里香专家,但我认为您无法使用row[index] 访问列表元素,也许是row.get(0)
  • @RC 也试过了。没有帮助。抛出 java.lang.IndexOutOfBoundsException:索引:1,大小:1
  • 实际上,该异常是说您的行没有多个值:语法row[0] 工作正常,row[1] 然后爆炸,因为该子中只有一个值列表。所以这可能是你真正的问题?

标签: java spring-boot thymeleaf


【解决方案1】:

双循环怎么样?

注意我不确定这个XHTML 的有效性,您可能必须在TR 下使用与SPAN 不同的HTML 元素)

<tr th:each="row : ${rows}">
      <span th:each="s : ${row}">
         <td th:text="${s}"></td>             
      </span>
 </tr>

【讨论】:

  • 太棒了!很高兴为您提供帮助!
  • 实际上,意图是使用要传递给javascript方法的行中的值。任何建议,例如,如何在不迭代的情况下使用它?所以,基本上我想使用第一三行值传递给 javascript 方法。
  • 查看这个问题的答案:stackoverflow.com/questions/25687816/…
【解决方案2】:

如果您只想为每个人生成一个TD,请在TD 上添加另一个for-each:

<tr th:each="row : ${rows}">
     <td th:each="rowValue : ${row}" th:text="${rowValue}"></td>
</tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 2013-10-26
    • 2017-10-28
    • 1970-01-01
    • 2017-10-18
    • 2012-09-04
    • 2018-07-17
    • 2013-05-17
    相关资源
    最近更新 更多