Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke direct methods or methods with arguments / variables / parameters in EL

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:

<h:outputText value="#{bean.list.size()}" /> 

Or invoking an action method with arguments:

<h:commandButton value="edit" action="#{bean.edit(item)}" /> 

This does not seem to work in my environment. It doesn't seem to like parentheses.

javax.el.ELException: Error Parsing: #{bean.list.size()}
com.sun.el.parser.ParseException: Encountered "("

like image 976
DD. Avatar asked Jul 19 '10 19:07

DD.


2 Answers

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:

<context-param>          <param-name>com.sun.faces.expressionFactory</param-name>     <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>    </context-param> 

Or, when you're using MyFaces:

<context-param>          <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>     <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>    </context-param> 

An alternative for your particular case is to use JSTL's fn:length:

<h:outputText value="#{fn:length(bean.list)}" /> 

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.

like image 120
BalusC Avatar answered Sep 18 '22 03:09

BalusC


BalusC's answer is correct, however, when you use maven, you should exclude el-api 1.0 transitive dependency like this:

<dependency>     <groupId>org.jboss.seam</groupId>     <artifactId>jboss-el</artifactId>     <version>2.0.0.GA</version>     <!-- exclude el-api 1.0 transitive dependency -->     <exclusions>         <exclusion>             <groupId>javax.el</groupId>             <artifactId>el-api</artifactId>         </exclusion>     </exclusions> </dependency> 
like image 35
Hrotkó Gábor Avatar answered Sep 17 '22 03:09

Hrotkó Gábor