0% found this document useful (0 votes)
26 views

08 ExpressionLanguage4

Expression Language (EL) is now a built-in feature of JSP 2.0 that provides a simpler syntax for accessing properties of JavaBeans. For example, ${item.price} can display the price property of an item object. EL also allows functions to be defined and invoked, such as ${fn:allCaps(lastName)} to call an allCaps() function. The document provides examples of how EL expressions can be used to replace scriptlets for retrieving values from JavaBeans or maps.

Uploaded by

Smita B.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

08 ExpressionLanguage4

Expression Language (EL) is now a built-in feature of JSP 2.0 that provides a simpler syntax for accessing properties of JavaBeans. For example, ${item.price} can display the price property of an item object. EL also allows functions to be defined and invoked, such as ${fn:allCaps(lastName)} to call an allCaps() function. The document provides examples of how EL expressions can be used to replace scriptlets for retrieving values from JavaBeans or maps.

Uploaded by

Smita B.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/31/2006

Expression Language

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

10/31/2006

Expression Language
?

Based on SPEL from JSTL 1.0

Simplest Possible Expression Language

Let you access the property values of a JavaBean in a simpler syntax

Example: ${item.price} Template text Attributes of any standard or custom action Extensible via tag libraries Example: ${fn:allCaps(lastName)} JSTL 1.1 provides 16 standard EL functions
2

Recognized by JSP container in:


Support for custom EL functions:


Expression language is based on Simplest Possible Expression Language from JSTL 1.0. It basically let you access the property values of a JavaBean in a simpler syntax. For example, price property value of item object can be displayed by using ${item.price} syntax. It also let you perform arithmetic operations, comparisons. You can also access the implicit objects such as form input parameters from the HTML client. The JSP expression language allows you to define a function that can be invoked in an expression. If you are familiar with string manipulation code, the functions serve somewhat similar purpose. The difference is that under JSP 2.0, these functions can be invoked using EL syntax. Functions are defined using the same mechanisms as custom tags. For example, if there is a function, for example, allCaps() method that is defined inside tag handler, you can call it through EL syntax, for example, fn prefix with the method name and parameter.

10/31/2006

Integrated Expression Language Example


?

Using scriptlets:
<center> <jsp:useBean id="foo" class="FooBean" /> <%= foo.getBar() %> </center>

Equivalent, using an EL expression:


<center> ${foo.bar} </center>
3

So this is a very simple comparison between JSP page that uses scriptlet and the one that uses expression languagedoing the same thing. In the upper part of the slide, we use scriptlet in order to retrieve the value of a property called Bar from a JavaBean called foo. So here you declare your JavaBean, foo, first and the use getter method, getBar(), to retrieve the value. In the bottom part of the slide, we use EL expression to do the same thing. So in order to get the value of bar property of the foo object, you just say ${foo.bar}.

10/31/2006

Integrated Expression Language Example


?

Using scriptlets:
<% Map m = (Map)pageContext.getAttribute("states" ); State s = ((State)m.get( "NY" )); if( s != null ) { %> <%= s.getCapitol() %> <% } %>

Equivalent, using an EL expression:


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

This is another example expression language, this time with a Map collection object in which multiple key-object pairs, in this case, state and its capital city pairs, are stored. Again, if you want to get the capital city of a particular state from states map object, you provide the key value with a bracket and the access the capital property.

You might also like