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

后端 未结 2 977
半阙折子戏
半阙折子戏 2020-11-22 01:57

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:



        
相关标签:
2条回答
  • 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>
    
    0 讨论(0)
  • 2020-11-22 02:20

    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.

    0 讨论(0)
提交回复
热议问题