Thymeleaf:无法访问日期
Posted
技术标签:
【中文标题】Thymeleaf:无法访问日期【英文标题】:Thymeleaf: Can't access the date 【发布时间】: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给你评论字段吗? 对不起,我想让它更方便一些,并排除了一些代码。现在有完整的课程。 我更新了答案 一点建议:跳过所有这些废话,看看龙目岛项目 【参考方案1】:我假设您的 getters
类字段没有 getters
。
Spring 表达式语言使用标准的 JavaBean 命名约定/语义。当您尝试访问comment.dateCreated
时,它将寻找comment.getDateCreated()
。如果您缺少带有私有字段的 getter 方法,它将假定您没有该字段。
public Date getDateCreated()
return this.dateCreated();
编辑
或者,如果您没有为访问器方法使用标准命名约定(看起来您没有,使用getDate()
来返回createdDate
字段),您可以使用SpEL
调用具有以下内容的方法.
// Change this
<div th:text="$#dates.format(comment.createdDate, 'dd-MM-yyyy')"></div >
// To this.
<div th:text="$#dates.format(comment.getDate(), 'dd-MM-yyyy')"></div >
【讨论】:
我更新了实体。很抱歉造成误导性的编辑。以上是关于Thymeleaf:无法访问日期的主要内容,如果未能解决你的问题,请参考以下文章
Ratpack + Thymeleaf + shadowJar - 解析模板“home”时出错,模板可能不存在或无法访问
Thymeleaf:对象中的对象,无法像jsp那样访问值?属性或字段 - 在 null 上找不到
SpringBoot学习------SpringBoot使用Thymeleaf模块访问不了静态页面