Expression Language (EL)"

From Documentation
(Created page with '{{ZKDevelopersGuidePageHeader}} *Overview *Access Java Bean *[[ZUML_Expression Lang…')
 
Line 1: Line 1:
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
  
*[[ZUML_Expression Language (EL)#Overview|Overview]]
+
==Overview==
*[[ZUML_Expression Language (EL)#Access_Java_Bean|Access Java Bean]]
+
Like JSP<ref>Tutorial for EL in JSP http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html</ref>, 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 <tt>></tt>,<tt>==</tt>,<tt>+</tt>,<tt>-</tt> , it can work with ZK attributes like <tt>if</tt> to provide sophisticated layout of components.
*[[ZUML_Expression Language (EL)#Access_ZUML_Component|Access ZUML Component]]
+
 
*[[ZUML_Expression Language (EL)#Access_Implicit_Object|Access Implicit Object]]
+
EL expressions use the syntax <tt>${expr}</tt>.
*[[ZUML_Expression Language (EL)#More_EL_Examples|More EL Examples]]
+
 
*[[ZUML_Expression Language (EL)#Import_Java_Method's|Import Java Method's]]
+
'''Notes'''
 +
<references/>
 +
 
 +
==Access Java Bean==
 +
 
 +
In the following example
 +
 
 +
<source lang="xml" >
 +
<element attr1=${bean.property}/>
 +
</source>
 +
 
 +
<tt>${bean.property}</tt> will be autowired to <tt>bean.getProperty()</tt>.
 +
 
 +
As you can see, developer can access Java Beans with EL intuitively.
 +
[[sample code for el access java bean | A full example]]
 +
 
 +
==Access ZUML Component==
 +
 
 +
The following is an example for referencing a component in an EL expression.
 +
 
 +
<source lang="xml" >
 +
<window>
 +
<textbox id="source" value="ABC"/>
 +
<label value="${source.value}"/>
 +
</window>
 +
</source>
 +
 
 +
You have to assign <tt>id</tt> for referenced component.
 +
 
 +
==Access Implicit Object==
 +
 
 +
In the following example, you can easily access implicit object like <tt>session</tt>
 +
 
 +
<source lang="xml" >
 +
<window>
 +
<label value="${session.deviceType}"/>
 +
</window>
 +
</source>
 +
 
 +
The browser shows the result: <tt>ajax</tt>.
 +
 
 +
==More EL Examples==
 +
<source lang="xml" >
 +
${empty myMap}
 +
${myMap[entry]}
 +
${3+counter}
 +
 
 +
</source>
 +
 
 +
Tip: <tt>empty</tt> is an operator used to test whether a map, a collection, an array or a string is null or empty.
 +
 
 +
Tip: <tt>myMap[entry]</tt> is a way to access an element of a map. In other words, it is the same as <tt>myMap.get(entry)</tt> 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.
 +
 
 +
<source lang="xml" >
 +
<window if="${some > 10}">
 +
</source>
 +
 
 +
Standard implicit objects, such as <tt>param</tt> and <tt>requestScope</tt>, and ZK implicit objects, such as <tt>self</tt> and <tt>page</tt>, are supported to simplify the use.
 +
 
 +
<source lang="xml" >
 +
<textbox value="${param.who} does ${param.what}"/>
 +
</source>
 +
 
 +
==Import Java Methods==
 +
You have two ways to import java methods to EL. Through <tt>xel-method</tt>, or <tt>taglib</tt>.
 +
 
 +
===Through <tt>xel-method</tt>===
 +
you can use a processing instruction called the <tt>xel-method</tt> as follows.
 +
 
 +
<source lang="xml" >
 +
<?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')}"/>
 +
</source>
 +
 
 +
In example above, <tt>Class.forName("java.util.List")</tt> is called.
 +
 
 +
===Through <tt>taglib</tt>===
 +
To import EL functions from TLD(TagLib Definition)<ref>http://java.sun.com/products/jsp/tutorial/TagLibraries17.html</ref> files, you could use a processing instruction called <tt>taglib</tt>
 +
as follows.<ref>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.</ref>
 +
 
 +
<source lang="xml" >
 +
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
 +
</source>
 +
 
 +
In the following example, we use function <tt>l</tt> to get the property <tt>app.title</tt> defined in resource file.<ref>It's a way to retrieve resources for different locale. Please refer to Chapter. [[Internationalization | Internationalization]]</ref>
 +
<source lang="xml" >
 +
<window title="${c:l('app.title')}">
 +
</source>
 +
 
 +
 
 +
Inside core.dsp.tld, you can find the definition of function <tt>l</tt>.
 +
<source lang="xml" >
 +
<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>
 +
</source>
 +
 
 +
 
 +
 
 +
'''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.
 +
 
 +
<references/>
 +
 
 +
== Quiz ==
 +
 
 +
1.Execute the following code, and tell what's wrong with the following code? What operator does EL accept?
 +
<source lang="xml" >
 +
<window>
 +
<button id="btn" label="OK"/>
 +
${btn.label=10}
 +
</window>
 +
</source>
 +
 
 +
 
 +
2.Rewrite [[sample code for el access java bean | A full example]] in Access Java Bean, add field age and gender.
  
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Revision as of 08:07, 12 July 2010

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.