我正在为使用JSF的应用程序开发一个评论部分
目前我正在将评论传递给数据库,并使用ajax将其呈现在评论输入下方
我现在正试图将评论发布的日期存储到数据库中的评论对象。对于注释文本,我将输入框的值分配给注释对象中的相应实体。我编写了一个返回当前日期和时间的方法,当用户添加注释时,我想调用此方法,设置它的值并将其传递给数据库。
这是我目前的代码。我无法弄清楚如何在JSF结束时为这个日期工作。谢谢你的帮助。
JSF页面:
<table>
<tr>
<td><div class="white"><H2>Comments</H2></div>
<h:inputTextarea rows="5" cols="55" id="comment" class="inputbox" value="#{commentBean.comment.commentText}"
size="20" /></td>
</tr>
<tr>
<td></td>
<!-- CODE I NEED TO SET THE DATE AUTOMATICALLY TO commentBean.comment.commentDate -->
<td><h:commandButton id="update"
action="#{commentBean.addComment(searchBean.companyId)}" class="myButton2" value="Add Comment">
<f:ajax execute="@form" render=":results" />
</h:commandButton></td>
</tr>
</table>
CommentBean:
public String addComment(int contractorId) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query myQuery = em.createQuery("SELECT u FROM BusinessAccount u WHERE u.id=:id");
myQuery.setParameter("id", contractorId);
List<BusinessAccount> accounts=myQuery.getResultList();
BusinessAccount account =accounts.get(0);
comment.setBusinessAccount(account); //managing both sides
account.getComments().add(comment); //managing both sides
em.persist(comment);
em.getTransaction().commit();
em.close();
comment.setCommentText(null);
return "success";
}
public String calculateCurrentDateTime() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime()).toString();
return currentDate;
}
评论对象类:
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int comment_id;
@Column(name = "comment_text")
private String commentText;
@Column(name = "date")
private String commentdate;
public String getCommentdate() {
return commentdate;
}
public void setCommentdate(String commentdate) {
this.commentdate = commentdate;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "contractor_id", referencedColumnName="id") })
private BusinessAccount businessAccount;
public BusinessAccount getBusinessAccount() {
if (businessAccount == null) {
businessAccount = new BusinessAccount();
}
return businessAccount;
}
public void setBusinessAccount(BusinessAccount businessAccount) {
this.businessAccount = businessAccount;
}
public int getComment_id() {
return comment_id;
}
public void setComment_id(int comment_id) {
this.comment_id = comment_id;
}
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
}
答案 0 :(得分:0)
当用户发布新评论时调用addComment(int )
,只需在保留评论对象之前调用并同时使用方法calculateCurrentDateTime()
,如下所示:
public String addComment(int contractorId) {
...
comment.setCommentdate(calculateCurrentDateTime()); // setting the comment's date property before adding it to the account object and before persisting
account.getComments().add(comment); //managing both sides
em.persist(comment);
...
}