Expression Language (EL)

From Documentation
Revision as of 08:07, 12 July 2010 by Maya001122 (talk | contribs)

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Overview

Like JSP[1], you could use Expression Language in ZUML pages. Expression Language is a scripting language. Through it, developer can write code in ZUML to access Java components (JavaBeans) easily. More than that, developer can access components in ZUML with simple and clear expression. EL can access implicit objects also. You can even import java methods to EL. Note that EL doesn't support "=" operator. Therefore, you can use EL to read value, but not change value. EL also supports operators like >,==,+,- , it can work with ZK attributes like if to provide sophisticated layout of components.

EL expressions use the syntax ${expr}.

Notes

Access Java Bean

In the following example

<element attr1=${bean.property}/>

${bean.property} will be autowired to bean.getProperty().

As you can see, developer can access Java Beans with EL intuitively. A full example

Access ZUML Component

The following is an example for referencing a component in an EL expression.

<window>
	<textbox id="source" value="ABC"/>
	<label value="${source.value}"/>
</window>

You have to assign id for referenced component.

Access Implicit Object

In the following example, you can easily access implicit object like session

<window>	
	<label value="${session.deviceType}"/>
</window>

The browser shows the result: ajax.

More EL Examples

${empty myMap}
${myMap[entry]}
${3+counter}

Tip: empty is an operator used to test whether a map, a collection, an array or a string is null or empty.

Tip: myMap[entry] is a way to access an element of a map. In other words, it is the same as myMap.get(entry) in Java.

When an EL expression is used as an attribute value, it could return any kind of objects as long as the component accepts it. For example, the following expression will be evaluated to a Boolean object.

<window if="${some > 10}">

Standard implicit objects, such as param and requestScope, and ZK implicit objects, such as self and page, are supported to simplify the use.

<textbox value="${param.who} does ${param.what}"/>

Import Java Methods

You have two ways to import java methods to EL. Through xel-method, or taglib.

Through xel-method

you can use a processing instruction called the xel-method as follows.

<?xel-method prefix="c" name="forName"
    class="java.lang.Class"    
    signature="java.lang.Class forName(java.lang.String)"?>    
<textbox value="${c:forName('java.util.List')}"/>

In example above, Class.forName("java.util.List") is called.

Through taglib

To import EL functions from TLD(TagLib Definition)[1] files, you could use a processing instruction called taglib as follows.[2]

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>

In the following example, we use function l to get the property app.title defined in resource file.[3]

<window title="${c:l('app.title')}">


Inside core.dsp.tld, you can find the definition of function l.

<function>
	<name>l</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.String getLabel(java.lang.String)</function-signature>
	<description>
		Returns the label of the specified key.
	</description>
</function>


Notes

The Developer's Reference provides more details on EL expressions. Or, you might refer to JSP 2.0 tutorials or guides for more information about EL expressions.
  1. http://java.sun.com/products/jsp/tutorial/TagLibraries17.html
  2. http://www.zkoss.org/dsp/web/core is not really an URL. It's a key to tell ZK loader to find the tld file inside ZK jar files.
  3. It's a way to retrieve resources for different locale. Please refer to Chapter. Internationalization

Quiz

1.Execute the following code, and tell what's wrong with the following code? What operator does EL accept?

<window>
	<button id="btn" label="OK"/>
	${btn.label=10}
</window>


2.Rewrite A full example in Access Java Bean, add field age and gender.



Last Update : 2010/07/12

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.