Page Initialization"

From Documentation
Line 18: Line 18:
  
 
=Initiator and EL=
 
=Initiator and EL=
To prepare a variable for EL expression in an initiator<ref>The preparation of variables for EL expression is generally better to be done in the constructor of <javadoc type="interface">org.zkoss.xel.VariableResolver</javadoc> (and specified with [[ZUML Reference/ZUML/Processing Instructions/variable-resolver|the variable-resolver directive]].</ref>, you could store the variable in page's attributes.
+
To prepare a variable for EL expression in an initiator, you could store the variable in page's attributes.
  
 
<blockquote>
 
<blockquote>
 
----
 
----
<references/>
+
'''Notice''' that the provision of variables for EL expression is generally better to be done with <javadoc type="interface">org.zkoss.xel.VariableResolver</javadoc> (and then specified it with [[ZUML Reference/ZUML/Processing Instructions/variable-resolver|the variable-resolver directive]]).
 
</blockquote>
 
</blockquote>
  

Revision as of 07:42, 26 November 2010


Page Initialization


Sometimes it is helpful to run some code before ZK Loader instantiates any component. For example, check if the user ha the authority to access, initialize some data, or prepare some variables for EL expressions.

This can be done easily by implementing Initiator, and then specifying it with the init directive.

<?init class="com.foo.MyInitial"?>

A typical use of the init directive is to specify a data binder, as shown below.

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

The data binder will parse all annotations and bind the data according to the annotations, after all components in a ZUML document is instantiated.

Initiator and EL

To prepare a variable for EL expression in an initiator, you could store the variable in page's attributes.


Notice that the provision of variables for EL expression is generally better to be done with VariableResolver (and then specified it with the variable-resolver directive).

For example, suppose we have a class, CustomerManager, that can be used to load all customers, then we could prepare a variable to store all customers as follows.

import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.Initiator;

public class AllCustomerFinder implements Initiator {
    public void doInit(Page page, Map args) throws Exception {
        String name = (String)args.get("name");
        page.setAttribute(name != null ? name: "customers", CustomerManager.findAll());
    }
    public void doAfterCompose(Page page) throws Exception { //nothing to do
    }
    public void doCatch(Throwable ex) throws Exception { //nothing to do
    }
    public void doFinally() throws Exception { //nothing to do
    }
}

Then, we could use the initiator in a ZUML document as follows.

 <?init class="my.AllCustomerFinder" name="customers"?>

 <listbox id="personList" width="800px" rows="5">
     <listhead>
         <listheader label="Name"/>
         <listheader label="Surname"/>
         <listheader label="Due Amount"/>
     </listhead>
     <listitem value="${each.id}" forEach="${pageCope.customers}">
         <listcell label="${each.name}"/>
         <listcell label="${each.surname}"/>
         <listcell label="${each.due}"/>
     </listitem>
 </listbox>

Version History

Last Update : 2010/11/26


Version Date Content
     



Last Update : 2010/11/26

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