2

I am seeing several questions asking how to pass parameters to JSF methods. With EL2.2 and servlet 3.0, it enables user to pass parameters into a method call. One example: How to pass an argument to method from rendered h:outputText? The question is: In JSF 2.x, each tag attribute is supposed to take either ValueExpression or MethodExpression. In the linked example, "rendered" is supposed to only take ValueExpression. However I do see it still works with MethodExpression if we put () to make it clear it is a method call instead of a property. Is this something the JSF 2.x spec just messed up or am I missing something?

Community
  • 1
  • 1
Baimai Wu
  • 118
  • 10

1 Answers1

1

Using the new EL 2.2 invoking-methods-with-arguments syntax in a value expression doesn't automagically make it a method expression or something. It's still a value expression. A value expression uses the returned value of the given EL expression as a value. A method expression invokes the given EL expression upon an action event associated with the component.

Whether the EL expression at its own invokes a property getter method or an arbitrary method with arguments really doesn't matter as to how the EL expression at the whole is treated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC, thank you very much for quick response. I have enjoyed your JSF blog and it helped me a lot. Your answer makes sense. However I still have confusion. The technology using EL (e.g. JSF 2.x) decides if the EL is treated as ValueExpression or MethodExpression. So if for JSF action attribute, I use action="aBean.aMethod()", and if the aMethod returns literal string "aBean.action", then are you saying action method in aBean will be invoked? Sounds to me in EL expression always get invoked first before JSF's mechanism, if we have () in EL, then it wom't use getter and instead call a method. – Baimai Wu Apr 18 '12 at 13:42
  • No, it's the tag handler which decides that on a per-attribute basis. You can see in the [tag documentation](http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/) how it's treated (check the "type" column, for example the [`h:commandButton`](http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/commandButton.html)). This does not depend on the EL syntax in any way. – BalusC Apr 18 '12 at 13:44
  • Agree, tag handler decides that. But the tag handler comes from JSF implementation. So it is JSF that decides that. I just modified the preivous comments. If you have time, can you elaborate? Thanks. – Baimai Wu Apr 18 '12 at 13:49
  • Method expressions are not value expressions. – BalusC Apr 18 '12 at 13:52
  • I did check the tag documentation before. Can you provide a link for "the new EL 2.2 invoking-methods-with-arguments syntax"? Is this specific for ValueExpression? If so, I guess this is where all my confusion comes from. – Baimai Wu Apr 18 '12 at 13:57
  • This is the link for EL from Jee 6 tutorial. http://docs.oracle.com/javaee/6/tutorial/doc/bnahu.html#gjhbz. The parameter passing into method call is not specific to ValueExpression. As a matter of fact, the syntax is totally different for ValueExpression and MethodExpression. So the question is how does this tag handler decides to call a method or a getter? I thought if tag handler decides it is a ValueExpression, it is going to call getter, if it is a MethodExpression, it is going to invoke the method. But I guess something is wrong here. – Baimai Wu Apr 18 '12 at 14:04
  • The new EL 2.2 syntax is not specific to value expressions. It applies to EL in general. You can use it in both value and method expressions. The difference between value and method expression is how the expression is ultimately been used. As a value which needs to be displayed/submitted, or as a method which needs to be invoked, as explained in the answer. – BalusC Apr 18 '12 at 14:07
  • You need to stop thinking that a value expression will always invoke the getter. This is not true. The value expression prints the result of the EL expression, regardless of the EL syntax. You can only not use EL 2.2 method syntax to submit values in input components, it must always point a property which has a getter and setter. – BalusC Apr 18 '12 at 14:12
  • Great. Appreciate your help and thanks in advance for your help in the future . Answer accepted. Have a great day:) – Baimai Wu Apr 18 '12 at 14:23