我需要将 comment 元素发送到 HTML。
没有comment DTO
,它实际上是TaskComment
。我在 Task 类中定义了这样的
@ElementCollection
private Set<TaskComment> comments;
在taskcomment
中,因为是model
类,所以有一些属性,例如:
private String message;
public String getUser() {
return user;
}
private ZonedDateTime createdAt = ZonedDateTime.now();
private String createdAtString;
我像这样定义params
Map<String, Object> params = prepareParams(task);
我将评论放入其中,如下所示:
params.put("commentsAdded", Collections.singletonList(comment));
没有任何问题,我可以看到我的 html,如下所示:
<tbody>
<tr th:each="addedComment : ${commentsAdded}">
<td th:text="${addedComment.user}">user...</td>
<td th:text="${addedComment.message}">message ...</td>
</tr> </tbody>
我不知道为什么我们要使用每个评论对象,只有一个评论对象。
但是当我想放置comment
的elementcollection时,它给出了错误。
@ElementCollection
private Set<TaskComment> comments;
我试过了
params.put("commentsInTask",new ArrayList(Arrays.asList(task.getComments())));
但在 html 中
<tr th:each="addedComment : ${commentsInTask}">
<td th:text="${addedComment.user}">user...</td>
<td th:text="${addedComment.message}">message ...</td>
对于
<td th:text="${addedComment.user}">user...</td>
该行给出了错误。我认为它在错误后停止了,可能也会给其他林带来错误。
我尝试放入singletonMap
,但它不是 HashMap 。这是 Collection 。
我试图把
params.put("commentsInTaskk",new ArrayList(Arrays.asList(task.getComments())));
但我无法让它工作。我认为这会起作用。或者我做错了。
你有什么建议?
我使用 spring boot + intellij。
例如,当我在 html 中使用它时
<tr th:each="addedComment : ${commentsInTask}">
<td th:text="${addedComment.key}">key ...</td>
<td th:text="${addedComment.value.user}">value.user...</td>
错误是:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "addedComment.key" (task_comment_cta:29)
最佳答案
我确实喜欢这个并且工作
<tr th:each="allComments : ${commentsInTask}">
<tr th:each="comment : ${allComments}">
<td th:text="${comment.user}">user ...</td>
关于java - 在 singletonlist 中发送 elementcollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100816/