08-ExpressionLanguage4

Embed Size (px)

Citation preview

  • 7/28/2019 08-ExpressionLanguage4

    1/4

    1

    Expression Language

    The first major enhancement in JSP 2.0 is integration of ExpressionLanguage (EL) as a built-in feature of JSP. Expression Language has beenaround as part of Java Standard Tag Library (JSTL) 1.0. Now due to itshuge success, EL is now part of JSP itself. That is, Servlet 2.4 and JSP 2.0compatible container should be able to handle EL expressions as native

    JSP syntax.

    10/31/2006

  • 7/28/2019 08-ExpressionLanguage4

    2/4

    2

    Expression Language? Based on SPEL from JSTL 1.0

    Simplest Possible Expression Language

    ? Let you access the property values of aJavaBean in a simpler syntax Example: ${item.price}

    ? Recognized by JSP container in: Template text

    Attributes of any standard or custom action

    ?

    Support for custom EL functions: Extensible via tag libraries

    Example: ${fn:allCaps(lastName)}

    JSTL 1.1 provides 16 standard EL functions

    Expression language is based on Simplest Possible ExpressionLanguage from JSTL 1.0. It basically let you access the property valuesof a JavaBean in a simpler syntax. For example, price property value ofitem object can be displayed by using ${item.price} syntax. It also let

    you perform arithmetic operations, comparisons. You can also access theimplicit objects such as form input parameters from the HTML client.

    The JSP expression language allows you to define a function that can beinvoked in an expression. If you are familiar with string manipulationcode, the functions serve somewhat similar purpose. The difference is thatunder JSP 2.0, these functions can be invoked using EL syntax.

    Functions are defined using the same mechanisms as custom tags. Forexample, if there is a function, for example, allCaps() method that isdefined inside tag handler, you can call it through EL syntax, for example,fn prefix with the method name and parameter.

    10/31/2006

  • 7/28/2019 08-ExpressionLanguage4

    3/4

    3

    Integrated ExpressionLanguage Example

    ? Using scriptlets:

    ? Equivalent, using an EL expression:

    ${foo.bar}

    So this is a very simple comparison between JSP page that usesscriptlet and the one that uses expression languagedoing thesame thing.

    In the upper part of the slide, we use scriptlet in order to retrievethe value of a property called Bar from a JavaBean called foo.So here you declare your JavaBean, foo, first and the use gettermethod, getBar(), to retrieve the value.

    In the bottom part of the slide, we use EL expression to do thesame thing. So in order to get the value of bar property of thefoo object, you just say ${foo.bar}.

    10/31/2006

  • 7/28/2019 08-ExpressionLanguage4

    4/4

    4

    Integrated ExpressionLanguage Example

    ? Using scriptlets:

    ?

    Equivalent, using an EL expression: ${states["NY"].capitol}

    ${states["NY"].capitol.name}

    This is another example expression language, this time with aMap collection object in which multiple key-object pairs, in thiscase, state and its capital city pairs, are stored.

    Again, if you want to get the capital city of a particular statefrom states map object, you provide the key value with a bracketand the access the capital property.

    10/31/2006