如何将方法名称传递给 actionListener

How to pass a method name to actionListner

我需要你的帮助来将正确的方法传递给按钮 actionListner。我在数据表中有一个按钮。按钮代码如下:

<h:commandButton id="submitButton" value="View" actionListener="#{bcd.showDatailDialog}" >
<f:setPropertyActionListener value="#{c}" target="#{bcd.selectedRequest}"/>
</h:commandButton>

根据datatable中显示的#{c.documentType}的值,决定在actionListner中使用哪个方法。

showDatailDialog方法代码为:

public String showDatailDialog(String sdoc) {

private String methodName; 

try {

if (sdoc.equalsIgnoreCase("CE")) {

mthodName="#{bcd.BCESS}";

}
else
{
mthodName="#{bcd.BSSES}";   
}

        } catch (Exception e) {
            System.err.print(e);
            e.printStackTrace();
        }
return methodName;
    }

现在当我点击按钮时,它显示了一个错误:

<ActionListenerImpl> <processAction> javax.el.MethodNotFoundException: //C:/../JDeveloper/../XXX.war/jsf/Search.jsf @91,89 action="#{bcd.showDetailDialog}": Method not found: rfc.Bean3@355f70.showDetailDialog() 

showDatailDialog 代码中的方法需要一个 String 参数,因此只需像这样使用它

actionListener="#{bcd.showDatailDialog(c.documentType)}"

您错误地解决了这个问题。 actionListener(和 action)属性必须表示方法表达式,而不是值表达式。现在您将其视为返回方法表达式的值表达式。这行不通。

只需让它调用一个真正的方法,该方法又进一步委托给所需的方法。

<h:dataTable ... var="item">
   ...
   <h:commandButton ... action="#{bean.action(item)}" />
public void action(Item item) {
    if (item.getType() == Type.FOO) { // It's of course an enum.
        oneMethod(item);
    } else {
        otherMethod(item);
    }
}

另请参阅:

  • How can I pass selected row to commandLink inside dataTable?
  • Is it possible to use EL conditional operator in action attribute?
  • Differences between action and actionListener