【问题标题】:Unable to take PrimeFaces p:rating value and save it into the Oracle database无法获取 PrimeFaces p:rating 值并将其保存到 Oracle 数据库中
【发布时间】:2014-06-29 09:11:42
【问题描述】:

我想使用 p:rating 组件来投票并将值保存到数据库中(如果它已经被投票以便以后能够从数据库中提取值)。在实体中,我也有 cmets 字段,我想如果 cmets 已经编写为仅添加投票,所以我有时会进行合并而不是持久化(如果数据库中没有写入此请求)。 我的 .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没有执行。它只在页面渲染时执行一次。我怎样才能实现这个工作?

【问题讨论】:

    标签: java jsf jsf-2 primefaces


    【解决方案1】:

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

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

    有用的链接

    【讨论】:

    • 感谢您的回复,但我的方法仍未执行。你知道为什么吗?
    • 我看到你已经接受了我的回答。最后,您是否能够调用侦听器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多