无法获取PrimeFaces p:rating值并将其保存到Oracle数据库中

时间:2014-06-29 09:11:43

标签: java jsf jsf-2 primefaces

我想使用p:rating组件来投票并将值保存到数据库中(如果它已被投票以便以后能够从db中提取值)。在实体中我也有注释字段,我想如果已经写了注释只添加投票,所以我有时会进行合并而不是持久化(如果在DB中写入此请求没有任何内容)。 我的.xhtml页面中有以下代码:

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

在我的豆里,我有:

@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不会执行。它只在呈现页面时执行一次。我该如何实现这项工作?

1 个答案:

答案 0 :(得分:1)

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

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

有用的链接