Search code examples
jsf-2facelets

Passing actionListener method names as arguments to include view


I'm trying to pass action parameters to the partial view in my page but for some reason it says that my parameter is null, even though it's the bean that is being used in the outer page.

Page:

<ui:include src="/templates/common/ajaxConfirmPopup.xhtml">
    <ui:param name="bean" value="#{paginaBean}" />
    <ui:param name="action" value="deleteAll" />
</ui:include>

Calling the action in the partial:

actionListener="#{bean[action]}"

But I get the following exception:

Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bean' resolved to null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:98) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.parser.AstValue.invoke(AstValue.java:244) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]

Am I doing something wrong?

NOTE: Everything works fine when method has ActionEvent parameter, it is not working when method is without parameters.


Solution

  • This problem is explainable if you have declared actionListener methods in EL 2.2 syntax without any arguments as follows all the time:

    actionListener="#{bean.method()}"
    

    which indeed expects an argumentless method. However, the standard JSF way is the following

    actionListener="#{bean.method}"
    

    which really expects an ActionEvent argument. Your question history indeed suggests that you were using EL 2.2 syntax all the time and thus you somehow assumed it to be "the standard" while that's not true.

    When parameterizing the actionListener method for an include template like as in your approach with actionListener="#{bean[action]}", the actionListener method is been interpreted as

    actionListener="#{bean.method}"
    

    which thus expects an ActionEvent argument.

    You have basically 3 options to fix this:

    1. Specify the ActionEvent argument in the method, no excuses.

    2. Replace actionListener by action. It's no problem to keep the method void. If e.g. Eclipse EL validation jerks, just tone it in preferences.

    3. Use EL 2.2 syntax like so actionListener="#{bean[action]()}". This however fails in early Tomcat and Glassfish versions.

    See also: