【发布时间】:2018-09-04 17:47:37
【问题描述】:
在这个 html 中:
<div class="container">
<div class="w-50">
<h1> MyBlog </h1>
<div><h3 class="font-weight-bold" th:text="${post.title}"></h3></div >
<div th:text="${post.content}"></div >
<div><span>Category: </span><span th:text="${post.category}"></span></div>
<div><span>Author: </span><span th:text="${post.signature}"></span></div >
<div th:text="${#dates.format(post.date, 'dd-MM-yyyy')}"></div >
</div>
<div class="w-50" th:each="comment : ${post.comments}">
<div th:text="${comment.contentCom}"></div>
<div th:text="${comment.author}"></div>
<div th:text="${#dates.format(comment.dateCreated, 'dd-MM-yyyy')}"></div >
</div>
</div>
在没有行的情况下:
<div th:text="${#dates.format(comment.dateCreated, 'dd-MM-yyyy')}"></div >
页面加载正常,所有字段都显示。否则我会收到状态 500 和错误:
SpelEvaluationException: EL1008E: 在“pl.reaktor.model.Comment”类型的对象上找不到属性或字段“dateCreated” - 可能不是公开的?
这些是实体:
帖子对象的博客:
package pl.reaktor.model;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="blog")
public class Blog {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="title")
private String title;
@Lob
@Column(name="content")
private String content;
@Column(name="category")
private String category;
@Column(name="signature")
private String signature;
@Column(name="createdate", updatable = false)
private Date date = new Date();
@Column(name="editdate")
private Date updateDate = new Date();
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE)
private List<Comment> comments;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
public Blog(){}
public Blog(Long id, String title, String content, String category, String signature, Date date) {
super();
this.id = id;
this.title = title;
this.content = content;
this.category = category;
this.signature = signature;
this.date = date;
this.updateDate = date;
}
}
评论:
package pl.reaktor.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="comments")
public class Comment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="comment_id")
private Long cid;
@Column(name="comment_author")
private String author;
@Column(name="comment_content")
private String contentCom;
@Column(name="date_created", updatable=false)
private Date dateCreated = new Date();
@ManyToOne
@JoinColumn(name="post", referencedColumnName="id")
private Blog post;
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContentCom() {
return contentCom;
}
public void setContentCom(String contentCom) {
this.contentCom = contentCom;
}
public Date getDate() {
return dateCreated;
}
public void setDate(Date date) {
this.dateCreated = date;
}
public Blog getPost() {
return post;
}
public void setPost(Blog post) {
this.post = post;
}
public Comment() {}
public Comment(String author, String contentCom, Date dateCreated) {
super();
this.author = author;
this.contentCom = contentCom;
this.dateCreated = dateCreated;
}
}
在这种情况下如何检索日期?
【问题讨论】:
-
你有getter给你评论字段吗?
-
对不起,我想让它更方便一些,并排除了一些代码。现在有完整的课程。
-
我更新了答案
-
一点建议:跳过所有这些废话,看看龙目岛项目
标签: jpa spring-boot thymeleaf