Expression Language (EL)"

From Documentation
m (correct highlight (via JWB))
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
 +
{{Old Version
 +
|url=http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/UI_Composing/ZUML/EL_Expressions
 +
|}}
  
 
==Overview==
 
==Overview==
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.
+
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 <code>></code>,<code>==</code>,<code>+</code>,<code>-</code> , it can work with ZK attributes like <code>if</code> to provide sophisticated layout of components.
  
EL expressions use the syntax <tt>${expr}</tt>.
+
EL expressions use the syntax <code>${expr}</code>.
  
 
'''Notes'''
 
'''Notes'''
 
<references/>
 
<references/>
  
==Access Java Bean==
 
  
In the following example
+
{{ZKDevelopersGuideHeadingToc}}
  
<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}}

Latest revision as of 10:36, 19 January 2022

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


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





Last Update : 2022/01/19

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