Chirag Loliyana
Chirag Loliyana

Reputation: 17

Primefaces p:menuitem pass an attributes to actionListener

I would like to pass some attributes to actionListener method.

My implementation is like...

<c:forEach items="${customerProductsBean.userProductList}" var="userProduct">
    <p:panel toggleable="#{true}" toggleSpeed="500" header="#{userProduct.product}" >
       // Some Code... Data Table and Tree Table

        <f:facet name="options">
            <p:menu>
                <p:menuitem value="ProductSetup" actionListener="#{customerProductsBean.getProductSetupData}" >
                      <f:attribute name="userIdParam" value="#{data.userId}"/>
                      <f:attribute name="geCustomerIdParam" value="#{data.geCustomerId}"/>
                      <f:attribute name="acpProductParam" value="#{data.acpProduct}"/>
                </p:menuitem>
                <p:menuitem value="Remove Product" url="#" onclick=""/>
            </p:menu>
        </f:facet>
    </p:panel>
</c:forEach>

And in Java Action Listener

public void getProductSetupData(ActionEvent actionEvent) {
      try {
          String userIdParam = 
     (String)actionEvent.getComponent().getAttributes().get("userIdParam");
          String geCustomerIdParam =
     (String)actionEvent.getComponent().getAttributes().get("geCustomerIdParam");
          String acpProductParam =
     (String)actionEvent.getComponent().getAttributes().get("acpProductParam");
      } catch(Exception e) {
           // Exception
      }
}

I tried it using <f:attribute> and <f:param> but was not able to get the value in Java.

In java It shows null for each value.

Upvotes: 0

Views: 12882

Answers (1)

BalusC
BalusC

Reputation: 1108742

This won't work if #{data} refers to the iterating variable of an iterating JSF component such as <h:dataTable var>. The <f:attribute> is set during JSF view build time, not during JSF view render time. However, the <h:dataTable var> is not available during view build time, it is only available during view render time.

If your environment supports EL 2.2, do instead

<p:menuitem ... actionListener="#{customerProductsBean.getProductSetupData(data)}" />

with

public void getProductSetupData(Data data) {
    // ...
}

Or if your environment doesn't, do instead

public void getProductSetupData(ActionEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    Data data = context.getApplication().evaluateExpressionGet(context, "#{data}", Data.class);
    // ...
}

Upvotes: 3

Related Questions