JSF 2: how to pass an action including an argument

2019-01-14 10:31发布

This is basically an extension to this answer.

I am trying to get an argument into a method/action call (for a delete button in a list/data table).

Client:

<ui:include src="...">
  <ui:param name="acceptButtonBean" value="#{repoHome}" />
  <ui:param name="acceptButtonAction" value="removeIndividualDocument(#{doc.id})" />
</ui:include>

Sub view:

<h:commandButton value="Continue"
                 action="#{acceptButtonBean[acceptButtonAction]}" />
  ...
</h:commandButton>

However, JSF fails with an exception saying:

...yadda, yadda
02:46:02,431 ERROR [stderr] (http--127.0.0.1-8080-5) Caused by: javax.el.MethodNotFoundException: /subviews/remove-doc-clink-popup.xhtml @37,98 action="#{acceptButtonBean[acceptButtonMethod]}": Method not found: com.company.project.beans.RepoHome@34b183e7.removeExternalDocument(89)()
02:46:02,431 ERROR [stderr] (http--127.0.0.1-8080-5)    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
02:46:02,431 ERROR [stderr] (http--127.0.0.1-8080-5)    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
02:46:02,431 ERROR [stderr] (http--127.0.0.1-8080-5)    ... 31 more

Note the

....RepoHome@34b183e7.removeExternalDocument(89)()

It can't work that way. JSF seems to append parentheses no matters what.

Can it be achieved differently, but still with the above technique? If so, how?

If not, why isn't it working? Is it specified? Is it a bug with Mojarra 2.0.x? I see no problem to omit the parentheses in case of the presence of other parentheses...

Note I'm not looking for alternative solutions like using f:param, f:attribute, or f:setPropertyActionListener.

Thanks in advance

1条回答
Summer. ? 凉城
2楼-- · 2019-01-14 10:41

That's indeed not valid EL. You cannot mix method names and arguments in a single variable. This should work:

<ui:include src="...">
  <ui:param name="acceptButtonBean" value="#{repoHome}" />
  <ui:param name="acceptButtonAction" value="removeIndividualDocument" />
  <ui:param name="acceptButtonArgument" value="#{doc.id}" />
</ui:include>

with

<h:commandButton value="Continue"
    action="#{acceptButtonBean[acceptButtonAction](acceptButtonArgument)}" />

Note that this is not exactly related to JSF, but to EL. If it were a bug or a feature, you'd need to read up the EL specification or report to the EL guys, not the JSF one. JSF is nothing to blame here. EL is an entirely standalone API which JSF just happens to use.


Update: it turns out that it works on Tomcat 7 (and likely any other container with org.apache.el.* implementation), but not on Glassfish 3 (with com.sun.el.* implementation). It fails as follows while displaying the page:

Caused by: javax.el.ELException: Error Parsing: #{p1[p2](p3)}
    at com.sun.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:174)
    at com.sun.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:191)
    at com.sun.el.lang.ExpressionBuilder.createMethodExpression(ExpressionBuilder.java:242)
    at com.sun.el.ExpressionFactoryImpl.createMethodExpression(ExpressionFactoryImpl.java:81)
    at org.jboss.weld.util.el.ForwardingExpressionFactory.createMethodExpression(ForwardingExpressionFactory.java:43)
    at org.jboss.weld.el.WeldExpressionFactory.createMethodExpression(WeldExpressionFactory.java:62)
    at com.sun.faces.facelets.tag.TagAttributeImpl.getMethodExpression(TagAttributeImpl.java:222)
    ... 63 more
Caused by: com.sun.el.parser.ParseException: Encountered "(" at line 1, column 9.
Was expecting one of:
        (*snip*)

I checked chapter 1.19 of the EL 2.2 spec:

 ValueSuffix      ::= ‘.’ Identifier MethodParameters?
                    | ‘[‘ Expression ‘]’ MethodParameters?          <-- Look here
 MethodParameters ::= '(' (Expression (‘,’ Expression )* )? ')'

and I am pretty confident that Tomcat is right. It's time to report a bug to Glassfish boys: GLASSFISH-17628.


Update 2: you seem to be actually using JBoss 7. I don't know what fork of Tomcat 7 exactly it uses, but I can confirm that I can reproduce your problem with Tomcat 7.0.19; it fails as follows after pressing the button:

Caused by: javax.el.MethodNotFoundException: /test.xhtml @22,62 action="#{p1[p2](p3)}": Method not found: com.example.Bean@2616aa35.submit(java.lang.String)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

I was using Tomcat 7.0.22 when it ran successfully, so it has been fixed somewhere between Tomcat 7.0.20 and 7.0.22.

查看更多
登录 后发表回答