java Primefaces p:menuitem 将属性传递给 actionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9924127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:55:02  来源:igfitidea点击:

Primefaces p:menuitem pass an attributes to actionListener

javajsfjsf-2primefaces

提问by Chirag Loliyana

I would like to pass some attributes to actionListener method.

我想将一些属性传递给 actionListener 方法。

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

在 Java 动作监听器中

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.

我尝试使用它<f:attribute><f:param>但无法在 Java 中获取该值。

In java It shows null for each value.

在java中它为每个值显示null。

回答by BalusC

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.

如果#{data}引用迭代 JSF 组件的迭代变量,例如<h:dataTable var>. 该<f:attribute>期间JSF视图生成时设置,而不是JSF视图中渲染时间。但是,<h:dataTable var>在视图构建期间不可用,它仅在视图渲染期间可用。

If your environment supports EL 2.2, do instead

如果您的环境支持 EL 2.2,请改为

<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);
    // ...
}