Passing parameters to PrimeFaces Star Rating component?

后端 未结 4 472
南旧
南旧 2021-01-12 06:16

I\'m trying to make use of the Star Rating component from PrimeFaces. However, it does not allow you to pass in parameters. That makes it impossible for me to do a lookup

相关标签:
4条回答
  • 2021-01-12 06:29

    f:viewParam lets you pass request parameters to bean properties

    <f:metadata>
      <f:viewParam name="myObjID" value="#{myObj.id}"/>
    </f:metdata>
    

    id gets set in the MyObj Bean on page load

    <p:rating rateListener="#{myObj.myRating}" />
    

    Since your bean has the id, when the rateListener method is called the id can be used to save the rating to the database

    0 讨论(0)
  • 2021-01-12 06:31

    I finally figured out how to do this...

    <h:form>
       <p:rating value="#{myAction.rating}" />
       <input type="hidden" name="selectedObj" value="#{myObj.id}" />
    </h:form>
    

    Then, in my action class, I'm able to get the value for selectedObj by doing this...

    String selectedObjID = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("selectedObj");
    

    Piece of cake!

    0 讨论(0)
  • 2021-01-12 06:47

    I solved it as follows:

    <h:form>
        <p:rating id="rate" value="#{userHomeControllerBean.rating}">
            <f:param name="contentId" value="#{sharedcontent.content.id}" />
            <p:ajax event="rate" listener="#{userHomeControllerBean.onrate}" update="rate" />
            <p:ajax event="cancel" listener="#{userHomeControllerBean.oncancel}" update="rate" />
        </p:rating>
    </h:form>
    

    In my backing bean, I'm able to get the value for contentId as follows:

    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    int contentId = Integer.parseInt(params.get("contentId"));
    
    0 讨论(0)
  • 2021-01-12 06:52

    have you tried to use f:setPropertyActionListener in your button so that you can send any parameter you want to your action class? or you have to do it inside the p:rating?

    or you can do something like

    private Rating rating;

    //getter -setter

    and in your action bean, you can access this value ((UIParameter)rating.getChildren().get(0)).getValue();

    if this is the case, you may be doing sth wrong as this shouldn't be the case while you are using JSF.

    just my two cents...

    0 讨论(0)
提交回复
热议问题