How can I in JSF 2.0 invoke direct methods or methods with arguments / variables / parameters in EL?
For example, getting the list size in EL:
In standard EL prior to EL 2.2 from Java EE 6 you cannot directly invoke methods like
#{bean.method()}
nor invoke methods with arguments like #{bean.method(arg1, arg2)
.
If upgrading to a EL 2.2 / Java EE 6 compliant container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) is not an option and you're currently using EL 2.1 / Java EE 5 (Tomcat 6, Glassfish 2, JBoss AS 4, etc), then your best bet is to install JBoss EL. JBoss EL is an EL 2.1 compliant implementation which supports the same features as EL 2.2. Installing JBoss EL is a matter of putting the jboss-el.jar in /WEB-INF/lib
and adding the following to the web.xml
, assuming that you're using Mojarra:
com.sun.faces.expressionFactory
org.jboss.el.ExpressionFactoryImpl
Or, when you're using MyFaces:
org.apache.myfaces.EXPRESSION_FACTORY
org.jboss.el.ExpressionFactoryImpl
An alternative for your particular case is to use JSTL's fn:length:
Another alternative is to add a getter to the bean which returns List#size()
or in some specific cases a custom EL function.
Please note thus that invoking methods with arguments in EL is not a JSF 2.0 specific feature. It's an EL 2.2 specific feature. EL 2.2 is part of Java EE 6, which JSF 2.0 is also part of. So it look like a JSF 2.0 specific feature, but it isn't. JSF 2.0 is backwards compatible with Servlet 2.5 / EL 2.1 which lacks this feature. On the other hand, JSF 1.x is forwards compatible with Servlet 3.0 / EL 2.2, so it would also be possible to use this feature in JSF 1.x then, also using JBoss EL on Servlet 2.5 / EL 2.1.