EL Expressions"

From Documentation
Line 68: Line 68:
  
 
For more information please refer to [[ZUML Reference/EL Expressions/Operators|Operators]] and [[ZUML Reference/EL Expressions/Literals|Literals]].
 
For more information please refer to [[ZUML Reference/EL Expressions/Operators|Operators]] and [[ZUML Reference/EL Expressions/Literals|Literals]].
 +
 +
=Connecting to Java World=
 +
EL expressions are evaluated at the server when the page is rendered. Thus, it is allowed to access:
 +
 +
* Components by [[ZK Developer's Reference/UI Composing/Component-based UI|using its ID]]
 +
* Variables defined in [[ZK Developer's Reference/UI Composing/ZUML/Scripts in ZUML|zscript]]
 +
* [[ZUML Reference/EL Expressions/Implicit Objects|Implicit objects]]
 +
 +
Furthermore, you could define a variable resolver to associate a name with an object, or map a function to a Java static method as described in the following.
 +
 +
==Variable Resolver==
 +
 +
If you would like to support many variables, you could implement a variable resolver: a class that implements <javadoc type="interface">org.zkoss.xel.VariableResolver</javadoc>.
 +
 +
<source lang="java">
 +
package foo;
 +
public class CustomerResolver implements org.zkoss.xel.VariableResolver {
 +
    public Object resolveVariable(String name) {
 +
        if ("customers".equals(name))
 +
            return Customer.getAll("*");
 +
//    if ("recent".equals(name))
 +
//        return something_else;
 +
        return null; //not a recognized variable
 +
    }
 +
}
 +
</source>
 +
 +
Then, you could specify it in a [[ZUML Reference/ZUML/Processing Instructions/variable-resolver|variable-resolver]] directive, such as:
 +
 +
<source lang="xml">
 +
<?variable-resolver class="foo.CustomerResolve"?>
 +
 +
<listbox>
 +
    <listitem label="${each.name}" forEach="${customers}"/>
 +
</listbox>
 +
</source>
 +
 +
==Associate with a Java Method==
 +
 +
The collection object could be retrieved by invoking a static method. For example, suppose we have a class and a static method as follows.
 +
 +
<source lang="java">
 +
package foo;
 +
public class Customer {
 +
    public Collection<Customer> getAll(String condition) {
 +
        //...returns a collection of customers
 +
    }
 +
    public String getName() {
 +
      return _name;
 +
    }
 +
    //...
 +
}
 +
</source>
 +
 +
Then, we could retrieve them with the [[ZUML Reference/ZUML/Processing Instructions/xel-method|xel-method]] directive:
 +
 +
<source lang="xml">
 +
<?xel-method prefix="c" name="getAllCustomers" class="foo.Customer"
 +
  signature="java.util.Collection getAll(java.lang.String)"?><!-- Generics not allowed -->
 +
<listbox>
 +
    <listitem label="${each.name}" forEach="${c:getAllCustomers('*')}"/>
 +
</listbox>
 +
</source>
 +
 +
==Associate with Multiple Java Methods==
 +
If you have several static methods, you could declare them in a XML file called taglib, such as
 +
 +
<source lang="xml">
 +
<taglib>
 +
<function>
 +
<name>getAllCustomers</name>
 +
<function-class>foo.Customer</function-class>
 +
<function-signature>
 +
java.util.Collection getAll(java.lang.String)
 +
</function-signature>
 +
<description>
 +
Returns a collection of customers.
 +
</description>
 +
</function>
 +
<!-- any number of functions are allowed -->
 +
</taglib>
 +
</source>
 +
 +
Then, you could use them by specifying it in a [[ZUML Reference/ZUML/Processing Instructions/taglib|taglib directive]].
 +
 +
<source lang="xml">
 +
<?taglib uri="/WEB-INF/tld/my.tld" prefix="my"?>
 +
<listbox>
 +
    <listitem label="${each.name}" forEach="${my:getAllCustomers('*')}"/>
 +
</listbox>
 +
</source>
  
 
=Version History=
 
=Version History=

Revision as of 03:17, 25 November 2010

Overview

An EL expressions is an expression enclosed with ${ and }, i.e., the syntax is ${expr}. For example,

 <element attr1="${bean.property}".../>
 ${map[entry]}
 <another-element>${3+counter} is ${empty map}</another-element>

When an EL expression is used as an attribute value, it could return any kind of objects as long as the attribute allows. For example, the following expressions will be evaluated to boolean and int, respectively.

 <window if="${some > 10}"><!-- boolean -->
   <progressmetter value="${progress}"/><!-- integer -->

If the class does not match, ZK Loader will try to coerce it to the correct one. If failed, an exception is thrown.

Multiple EL expressions could be specified in a single attribute:

<window title="${foo.name}: ${foo.version}">

Example

EL Expression Result
${1 > (4/2)} false
${100.0 == 100} true
${'a' < 'b'} true
${'hip' gt 'hit'} false
${1.2E4 + 1.4} 12001.4
${3 div 4} 0.75
${10 div 4} 2
${empty param.add} true if the request parameter named add is null or an empty string
${param['mycom.productId']} The value of the request parameter named mycom.productId

Difference from Java

  • A string can be enclosed with single quote or double double. In other words, 'abc' and "abc" are the same.
  • The empty operator is useful for testing null and empty (string, list and map), such as ${empty param.add}.
  • The . operator can be used to access a property of an object (assuming there is a get method of the same name), or a value of a map, such as ${foo.value.name}.
  • The [] operator can be used to access an item of a list or array, a value of a map, and a property of an object (assuming there is a get method of the same name), such as ${ary[5]} and ${wnd['title']}.
  • null is returned if the value is not found and the index is out-of-bound.

For more information please refer to Operators and Literals.

Connecting to Java World

EL expressions are evaluated at the server when the page is rendered. Thus, it is allowed to access:

Furthermore, you could define a variable resolver to associate a name with an object, or map a function to a Java static method as described in the following.

Variable Resolver

If you would like to support many variables, you could implement a variable resolver: a class that implements VariableResolver.

package foo;
public class CustomerResolver implements org.zkoss.xel.VariableResolver {
    public Object resolveVariable(String name) {
        if ("customers".equals(name))
            return Customer.getAll("*");
//     if ("recent".equals(name))
//         return something_else;
        return null; //not a recognized variable
    }
}

Then, you could specify it in a variable-resolver directive, such as:

<?variable-resolver class="foo.CustomerResolve"?>

<listbox>
    <listitem label="${each.name}" forEach="${customers}"/>
</listbox>

Associate with a Java Method

The collection object could be retrieved by invoking a static method. For example, suppose we have a class and a static method as follows.

package foo;
public class Customer {
    public Collection<Customer> getAll(String condition) {
        //...returns a collection of customers
    }
    public String getName() {
       return _name;
    }
    //...
}

Then, we could retrieve them with the xel-method directive:

<?xel-method prefix="c" name="getAllCustomers" class="foo.Customer"
   signature="java.util.Collection getAll(java.lang.String)"?><!-- Generics not allowed -->
<listbox>
    <listitem label="${each.name}" forEach="${c:getAllCustomers('*')}"/>
</listbox>

Associate with Multiple Java Methods

If you have several static methods, you could declare them in a XML file called taglib, such as

<taglib>
	<function>
		<name>getAllCustomers</name>
		<function-class>foo.Customer</function-class>
		<function-signature>
	java.util.Collection getAll(java.lang.String)
		</function-signature>
		<description>
	Returns a collection of customers.
		</description>
	</function>
	<!-- any number of functions are allowed -->
</taglib>

Then, you could use them by specifying it in a taglib directive.

<?taglib uri="/WEB-INF/tld/my.tld" prefix="my"?>
<listbox>
    <listitem label="${each.name}" forEach="${my:getAllCustomers('*')}"/>
</listbox>

Version History

Last Update : 2010/11/25


Version Date Content
     



Last Update : 2010/11/25

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