Version 18

    Expression Language FAQ

     

    What is an Expression Language (EL)?

    The Java EL 2.1 specification defines an Expression Language as "(a language that) makes it easy for page authors to access and manipulate application data without having to master the complexity associated with programming languages such as Java and JavaScript."  In practice, the Java EL provides a shorthand way to look up JavaBeans, call getters, call setters, and call other methods.

     

    There are other Expression Languages, but this FAQ only deals with the EL as defined by various Java JSRs.

     

    How about an example?

    Say I have a Person JavaBean with a "name" property.  It would have methods getName() and setName(String name).  Also assume that an instance of this JavaBean lives in some scope such as session scope or application scope.  The instance is bound to the name "employee".  I could refer to the name property like this:

    ${employee.name} 

     

    If "Name" were a class instead of a simple String, I could make the expression longer to get to its parts:

    First Name: ${employee.name.firstname}
    Last Name: ${employee.name.lastname}
    

     

    If I use this with a technology such as JSF, I can refer to it in a JSF tag like this:

    <h:inputText value="#{employee.name}"></h:inputText>

    In this case, JSF is responsible for using the expression to retrieve the value of "name" when the page is displayed and set the value of "name" when the page is submitted.

     

    What else can the EL do?

    Besides referring to getters, setters, and other methods, the EL has the ability to evaluate arithmetic and boolean expressions:

    ${employee.salary > 50000} 
    ${employee.salary * tax.rate} 
    ${employee.spouse == Empty} 
    ${employee.name.firstname == 'Fred'} 

     

    What are the Java EL specifications?

    There are three different EL specifications.

    The EL's for JSP 2.0 and JSF 1.1 were not compatible.  The Unified EL brings these EL's together under one specification.  The Unified EL was created by the JSP 2.1 expert group (JSR-245), but it is its own separate specification.

     

    What technologies use which EL?

    JSP 2.0

    JSP EL

    JSP 2.1

    Unified EL

    JSF 1.1

    JSF EL

    JSF 1.2

    Unified EL

    JSTL 1.1

    JSP EL

    JSTL 1.2

    Unified EL

    Facelets

    Unified EL

    JBoss Seam

    Depends on which view technology is used (JSP 2.0, JSP 2.1, or Facelets)

     

    Why do some expressions start with pound and others start with dollar sign?

    For the EL specification itself, there is no difference.  It is up to the technology using the EL to decide what it means.  For both JSP and JSF, expressions that start with a pound sign mean deferred evaluation and a dollar sign means immediate evaluation.  This all has to do with when the expression will actually be evaluated during request processing.  The pound sign is used in JSF components because we want the expression to be evaluated by the JSF lifecycle and not by the JSP or Facelets engine.

     

    What is the difference between a MethodExpression and a ValueExpression?

    MethodExpression and ValueExpression are classes defined by the Unified EL.  A MethodExpression invokes an arbitrary method on an object.  A ValueExpression only deals with getters and setters.  In JSF 1.1, we had MethodBinding and ValueBinding that did almost the same thing. 

     

    It is not always obvious from the expression whether it is a MethodExpression or ValueExpression.  However, the technology using the EL knows which kind it is based on the context.

     

    For instance, JSF knows that this should always be treated as a ValueExpression:

    <h:inputText value="#{employee.name}"></h:inputText>

    And, this should always be treated as a MethodExpression:

    <f:actionListener binding="#{foo.listen}" ></f:actionListener> 

    What's more, JSF knows what parameters should be sent to the "listen" method.

     

    Can I specify parameters to a MethodExpression myself?

    According to the specification, only the technology using the EL can pass parameters during evaluation of a MethodExpression.  However, JBoss Seam 1.1 adds the ability to pass params in the expression itself.  To use it, just put your parameters inside parens:

     <h:commandButton value="Submit" action="#{myinstance.save('name', address)}" ></h:commandButton> 

    In this example, "name" is a String literal and "address" is a ValueExpression. 

     

    To use action params with Facelets, you need to set up the SeamFaceletsViewHandler.  See the JBoss Seam 1.1 documentation for more details on this feature.

     

    What is an EL Variable?

    The Unified EL defines something called an EL Variable.  This is a variable that is used in an expression to point to some other expression.  It is typically used for loop control variables in tags such as JSTL's <c:forEach>. 

    <c:forEach var="item" items="#{inventory.allitems}">
       <h:outputText value="#{item.name}"></h:outputText>
    </c:forEach>
    

    In the above example "item" is an EL Variable because under the covers, it actually refers to the expression:

     #{inventory.allitems[index]} 

    It is stored in the ELContext instead of living in some ordinary scope.

     

    See the Unified EL Spec and the VariableMapper class in the JSP 2.1 JavaDoc for more details on EL Variables.

     

    What is an EL Function?

    An EL Function maps to a static method.  It is not used in JSF but it can be used in a JSP.  The function is typically defined in a TLD.

     

    See the Unified EL Spec and the FucntionMapper class in the JSP 2.1 JavaDoc for more details on EL Functions.

     

    It's cool how the EL automatically searches different scopes to find my domain objects.  Can it look in other places besides PageContext, request, session, and application?

    Yes.  In fact, that is a big part of the secret sauce in JBoss Seam.  You can extend the EL to resolve names in any context you can think of (JMX, LDAP, EJB3, etc.).  You extend the EL by implementing an ELResolver.  See the JSP 2.1 JavaDoc for details on the ELResolver interface.  The ELResolver is typically installed in a ServletContextListener.  If using JSF 1.2, it can be installed using the <el-resolver> tag in faces-config.xml.

     

    See Also:

     

    Referenced by: