无法迭代List <list <string>&gt;使用Thymeleaf / Spring-Boot

时间:2016-11-02 15:13:40

标签: java spring-boot thymeleaf

使用Thymeleaf,我无法迭代List&lt; List&lt; String&gt;&gt;

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

每行的值超过1个。

渲染时,我收到以下异常

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

提前致谢。

2 个答案:

答案 0 :(得分:2)

双循环怎么样?

注意,我不确定此XHTML的有效性,您可能需要使用不同的HTML元素,而不是SPAN下的TR元素。 <tr th:each="row : ${rows}"> <span th:each="s : ${row}"> <td th:text="${s}"></td> </span> </tr>

doSomething(name) {
    let _state = "photo" + name;

    console.log(this.state[_state]) // it work !
}

答案 1 :(得分:1)

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

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