JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?
Asked Answered
L

1

2

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

[email protected](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

Linsk answered 4/11, 2011 at 2:21 Comment(1)
By the way, curious that the error message used RepoHome#toString() instead of RepoHome#getClass().Eld
E
17

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: [email protected](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.

Eld answered 4/11, 2011 at 2:43 Comment(7)
I think this was one of my first tries, unfortunately it doesn't work: Caused by: javax.el.MethodNotFoundException: /subviews/remove-doc-clink-popup.xhtml @37,98 action="#{acceptButtonBean[acceptButtonAction](acceptButtonArgument)}": Method not found: com.company.project.beans.RepoHome@2f3e7ad6.removeIndividualDocument()Linsk
BTW Eclipse also shows a warning at the opening parenthesis here: EL syntax error: Unexpected symbol '('.Linsk
Funny, Glassfish 3.1.1 (com.sun.el.*) indeed jerks like that, but Tomcat 7.0.22 (org.apache.el.*) doesn't. Let me look ... As to Eclipse, I've turned off its EL validator, because it's an epic fail and annoys me all the time, so I can't tell anything about this.Eld
Thanks for helping, now I only need to find out which implementation JBoss AS 7 uses and report a bug there. Do you mind to share a link to your Glassfish bug report? ;-)Linsk
I did. But I now realize that it's actually not exactly he same exception. Let me try with older Tomcat versions...Eld
FWIW: Glassfish bug has just been fixed: java.net/jira/browse/GLASSFISH-17628Eld
In order to invoke a listener, where i included a p:confirmDialog in the ui:composition page, the following syntax worked for me: this worked: listener="#{bean.setModel(item)}" this didn't: listener="#{bean[setModel](item)}" in short: #{bean.method(param)} worked #{bean[method](param)} didn't (i am using pf 5)Oakman

© 2022 - 2024 — McMap. All rights reserved.