Вы находитесь на странице: 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.

Вам также может понравиться