Thymeleaf:无法访问日期

时间:2018-03-26 14:28:46

标签: jpa spring-boot thymeleaf

在这个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:属性或字段&#39; dateCreated&#39;在类型对象上找不到&#39; pl.reaktor.model.Comment&#39; - 也许不公开?

这些是实体:

博客对象:

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;
}

}

如何在这种情况下检索日期?

1 个答案:

答案 0 :(得分:1)

我假设您的getters类字段没有Comment

Spring Expression Language使用标准的JavaBean命名约定/语义。当您尝试访问comment.dateCreated时,它会查找comment.getDateCreated()。如果您缺少具有私有字段的getter方法,则会假设您没有该字段。

public Date getDateCreated() {
    return this.dateCreated();
}

修改

或者,如果您没有为访问者方法使用标准命名约定(看起来不是,使用getDate()返回createdDate字段),则可以使用{{ 1}}以下内容。

SpEL
相关问题