java - Primefaces p :menuitem pass an attributes to actionListener

标签 java jsf jsf-2 primefaces

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

我的实现就像......

<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>

在 Java Action 监听器中

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
      }
}

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

在java中,每个值都显示为null。

最佳答案

如果#{data},这将不起作用指迭代 JSF 组件的迭代变量,例如 <h:dataTable var><f:attribute>在 JSF View 构建时设置,而不是在 JSF View 渲染时设置。然而,<h:dataTable var>在 View 构建期间不可用,它仅在 View 渲染期间可用。

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

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

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

或者,如果您的环境没有,请改为这样做

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

关于java - Primefaces p :menuitem pass an attributes to actionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924127/

相关文章:

Java服务器页面: How to get a value into an attribute from code without Expression Language?

java - 该模块尚未部署[netbeans+glassfish]

java - JSF 2.0 h :selectManyListbox f:selectItems - always empty

jsf - 每次我在 JSF2 中发出 Ajax 请求时,我都会得到一个新的 session bean,为什么?

java - 如何检查用户输入的字符串是否包含文件扩展名

java - 数据库不会更新编辑文本更改android

java - 如何在spring data jpa查询中指定@lock超时?

java - 在扩展类上添加 'throws' ?

jsf - 在 <ui :repeat>? 中指定元素的条件渲染 <c:if> 似乎不起作用

jsf - 评估是否设置了 MethodExpression 属性(获取 PropertyNotFoundException)