进料
package com.v2.search.p;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "feeds")
public class feed {
@Id
private Integer feed_id;
public Integer getFeed_id() {
return feed_id;
}
public void setFeed_id(Integer feed_id) {
this.feed_id = feed_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public List<Comment> getComment() {
return comment;
}
public void setComment(List<Comment> comment) {
this.comment = comment;
}
@Field(type = FieldType.Text, name = "title")
private String title;
@Field(type = FieldType.Text, name = "body")
private String body;
@Field(type= FieldType.Nested , name="comment")
private List<Comment> comment;
}
子字段=注释
package com.v2.search.p;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Comment {
@JsonProperty("c_id")
private String c_id;
@JsonProperty("feed_id")
private String feed_id;
@JsonProperty("c_text")
private String c_text;
public String getC_id() {
return c_id;
}
public void setC_id(String c_id) {
this.c_id = c_id;
}
public String getFeed_id() {
return feed_id;
}
public void setFeed_id(String feed_id) {
this.feed_id = feed_id;
}
public String getC_text() {
return c_text;
}
public void setC_text(String c_text) {
this.c_text = c_text;
}
}
<table id="feeds" border="1" class="table table-striped table-responsive-md">
<thead class="table-dark">
<tr>
<th>Id</th>
<th>body</th>
<th>title</th>
<th>comment 1</th>
<th>comment 1a</th>
<th>comment 1b</th>
</tr>
</thead>
<tbody>
<tr th:each="feedDocument : ${listfeedDocuments}">
<td th:text="${feedDocument.feed_id}"></td>
<td th:text="${feedDocument.title}"></td>
<td th:text="${feedDocument.body}"></td>
<td th:each ="cd : ${feedDocument.comment}"</td>
<td th:text="${cd.get(0).c_text}"></td>
<td th:text="${cd.get(1).c_text}"></td>
</tr>
</tbody>
</table>
listfeedDocument包含字段和一个注释数组列表,该列表在另一个类中定义,具有一些属性。我想迭代另一个循环(如cd),以获得注释数组列表的值,其中包含属性(如c_text、c_id),但它显示了以下错误:**评估SpringEL表达式时出现异常:";cd.get(0).ctext"**
通过这种方式,我们可以在thymelaf中迭代数组列表,并获得我们想要的结果
<!--this is my text for comments iteration -->
<div th:each="feedDocument : ${listfeedDocuments}">
<th:block th:each="tx : ${feedDocument}">
<th:block th:each="x: ${tx.comment}">
<div th:text="${x.c_text}"></div>
</th:block>
</th:block>
</th:div>