2

I have multiple criterias for a particular product on which user can rate products.I am planning to use PrimeFaces rating component. To do this i have put rating component in <c:forEach> loop but when i submit the changes i have done the collection is passed empty.

Below is my code:

productrating.xhtml

 <h:form>
      <c:forEach items="#{productManagedBean.reviewxcriterias}" var="item">
          <h6>#{item.categoryRatingCriteriaId.ratingCriteriaId.criteriaName}</h6>
          <p:rating value="#{item.rate}" />
      </c:forEach>
      <p:commandButton value="Submit" action="#{productManagedBean.addReview}"/> 
 </h:form>

ProductManagedBean.java

@Named(value = "productManagedBean")
@RequestScoped
public class ProductManagedBean {

private Collection<Reviewxcriteria> reviewxcriterias;

public Collection<Reviewxcriteria> getReviewxcriterias() {
    return reviewxcriterias;
}

public void setReviewxcriterias(Collection<Reviewxcriteria> reviewxcriterias) {
    this.reviewxcriterias = reviewxcriterias;
}

public void onrate(RateEvent<Integer> rateEvent) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Rate Event", "You rated:" +    rateEvent.getRating());
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public void oncancel() {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cancel Event", "RateReset");
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public void addReview() {
    for (Reviewxcriteria r : reviewxcriterias) {
        System.out.println(r.getRate());
    }
  }
}

I have also tried with <p:ajax> as below but no luck. If i put <p:rating> outside <c:forEach> then it is working fine.

<h:form>
    <c:forEach items="#{productManagedBean.reviewxcriterias}" var="item">
          <h6>#{item.categoryRatingCriteriaId.ratingCriteriaId.criteriaName}</h6>
               <p:rating value="#{item.rate}">
                     <p:ajax event="rate" listener="#{productManagedBean.onrate}" update="messages" />
                     <p:ajax event="cancel" listener="#{productManagedBean.oncancel}" update="messages" />
               </p:rating>
    </c:forEach>
</h:form>
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • 1
    Tried `` instead of ``? – areus May 03 '20 at 10:11
  • @areus Also tried with but not working. – moheet jariwala May 03 '20 at 10:41
  • 1
    I tried your code (without ajax) and it works, I only remove the h6 value, because I don't know your bean definition, and I also add a populate of the list. Since you're using request scope pay attention when do you want to access those values, or maybe try a different one(view scope?). – WoAiNii May 03 '20 at 12:04
  • 1
    *"but when i submit the changes i have done the collection is passed empty"* Your problem matches #4 of abovelinked duplicate. – BalusC May 03 '20 at 13:06

0 Answers0