4

I'm trying to invoke a parameterless method from a JSF 2.0 facelet by

#{myBean.foo()}

(without any surrounding tag).

According to Burns/Schalk: The Complete Reference: JSF 2.0 that's possible (page 126, #{userBean.pullValuesFromFlash( )}).

However the framework takes the expression to be a value expression and thus thinks that foo should be a bean property. On JBOSS 7.0.1 (and 6, too) I get a

"The class '...' does not have the property 'foo'"

error message.

3 Answers 3

2

Judging by this response on the JBoss forum, method expressions must only be used in attributes that support them.

Stan Silvert wrote:

It looks to me like this is working as expected. This has nothing to do with a lack of arguments. Your expression, #{elManagedBean.hello()} is being treated as a ValueExpression. If you changed your method to getHello() then it would work. The question is, should it be treated as a ValueExpression or a MethodExpression? For instance, if you had the same expression in an action attribute it would be treated as a MethodExpression.

<h:commandButton value="Hello" action="#{elManagedBean.hello()}" 
     id="submit_button"/>

You have put the expression in the middle of the Facelets page and not as the value of an attribute. As far as I know, this will always be treated as a ValueExpression. I don't see how this would work in Glassfish. It's possible that there is some code that tries it out as a ValueExpression and then tries it as a MethodExpression if it fails. However, I think that would go against the EL spec. In other words, I'm surprised that this would work on Glassfish.

Sign up to request clarification or add additional context in comments.

Comments

2

McDowell has answered the cause of the problem: inline expressions are treated as value expressions, not as method expressions.

As to how to achieve the functional requirement anyway, use <f:event>.

<f:event type="preRenderView" listener="#{myBean.foo}" />

This will invoke the method right before render response.

3 Comments

It seems that 'el' tag wiki page has a bad example. Namely '${bean.list.size()}'. It will not work for two reasons: first - because inline expressions are treated as value expressions and second - javax.el.ListELResolver' works first and tries to get item in list by index 'size' (fails with NumberFormatException).
You're right, the particular example was bad especially because ListELResolver was been used. I fixed the wiki to remove it. But the other reason is not the case, because the method is not void.
Wiki looks ok know. But regarding inlined el expressions. When method has parameters then it is invoked (e.g. ${bean.find(param.id)}), if no parameters then resolver tries to find a property (${bean.find()} will fail because there is no getFind method, irrespective of find() return type). Interesting what is in el spec regarding that.
0

This depends on the EL version you are using on your servlet container. If using Tomcat 6, EL 2.1 is included and this does not support '()' as MethodExpression if the expression is in the middle of the Facelets page. Tomcat 7, which includes EL 2.2 does support this and even enhanced features as being able to pass parameters to the method expression:

So you do this:

<h:outputText value="#{object.test(10)}" ></h:outputText>

And receive the parameter in your bean (extra conversion and validation might be needed):

public String test(MyObject o)
{
    ...
    return value;
}

References: http://tomcat.apache.org/whichversion.html Using EL 2.2 with Tomcat 6.0.24

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.