gpt4 book ai didi

java - 无法获取 PrimeFaces p :rating value and save it into the Oracle database

转载 作者:行者123 更新时间:2023-12-01 12:48:15 24 4
gpt4 key购买 nike

我想使用 p: rating 组件来投票并将该值保存到数据库中(如果它已经被投票为能够稍后从数据库中提取该值)。在实体中,我也有评论字段,我希望是否已经编写了评论以仅添加投票,所以我有时会进行合并而不是保留(如果数据库中没有写入此请求的任何内容)。我的 .xhtml 页面中有以下代码:

<h:form
rendered="#{not empty userRequestBean.request.hiredAgency.city}">
<p:rating value="#{userRequestBean.rating}" stars="10"
onRate="#{userRequestBean.rateAgency()}">
</p:rating>
</h:form>

在我的 bean 中,我有:

@ManagedBean(name = "userRequestBean")
@SessionScoped
public class UserRequestBean implements Serializable {
private Integer rating; // Plus get and set methods
private TComment agencyComment = new TComment();
public void rateAgency() {
if (rating == null) return;
EntityManager em = HibernateUtil.getEntityManager();
if (!em.getTransaction().isActive())
em.getTransaction().begin();
Query queryAgencyComment = em.createQuery("select comment "
+ "from TRequest req join req.requestComments comment "
+ "where req.id = :requestId ");
Long requestId = (Long) request.getId();
queryAgencyComment.setParameter("requestId", requestId);
agencyComment = (TComment) queryAgencyComment.getSingleResult();

if (agencyComment.equals(null)) {
agencyComment = new TComment();
agencyComment.settRequest(request);
agencyComment.setCommentDate(new Date());
agencyComment.setAssessment(rating);
em.persist(agencyComment);
em.getTransaction().commit();
} else {
agencyComment.setAssessment(5);
em.merge(agencyComment);
em.getTransaction().commit();
}
}

但是onRate,rateAgency方法并没有被执行。它仅在页面呈现时执行一次。我该如何实现这项工作?

最佳答案

当您尝试运行服务器方法时,属性onRate 定义JavaScript(客户端)操作。您需要一个 ajax 操作:

<p:rating value="#{userRequestBean.rating}" stars="10" >
<p:ajax event="rate" listener="#{userRequestBean.rateAgency()}" />
</p:rating>

有用的链接:

关于java - 无法获取 PrimeFaces p :rating value and save it into the Oracle database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com