ZUML"

From Documentation
Line 28: Line 28:
  
 
{{ZKDevelopersReferenceHeadingToc}}
 
{{ZKDevelopersReferenceHeadingToc}}
 
=Basic Rules=
 
 
If you are not familiar with XML, please take a look at [[ZK Developer's Reference/UI Composing/ZUML/XML Background|XML Background]] first.
 
 
==A XML Element Represents a Component==
 
 
Each XML element represents a component, except special elements like <zk> and <attribute>. Thus, the following will cause three components (window, textbox and button) being created when ZK Loader processes it.
 
 
<source lang="xml">
 
<window>
 
  <textbox/>
 
  <button/>
 
</window>
 
</source>
 
 
In additions, the parent-child relationship of the created components will follow the same hierarchical structure of the XML document. In the previous example, window will be the parent of textbox and button, while textbox is the first child and button is the second.
 
 
===Special XML Elements===
 
 
There are a few elements dedicated to special functionality rather than a component. For example,
 
 
<source lang="xml" >
 
<zk>...</zk>
 
</source>
 
 
[[ZUML Reference/ZUML/Elements/zk|The zk element]] is a special element used to aggregate other components. Unlike a real component (say, <tt>hbox</tt> or <tt>div</tt>), it is not part of the component tree being created. In other words, it doesn't represent any component. For example,
 
 
<source lang="xml" >
 
<window>
 
    <zk if="${whatever}">
 
        <textbox/>
 
        <textbox/>
 
    </zk>
 
</window>
 
</source>
 
 
is equivalent to
 
 
<source lang="xml" >
 
<window>
 
    <textbox if="${whatever}"/>
 
    <textbox if="${whatever}"/>
 
</window>
 
</source>
 
 
For more information about special elements, please refer to [[ZUML Reference/ZUML/Elements|ZUML Reference]].
 
 
==A XML Attribute Assigns a Value to a Component's Property or Event Listener==
 
 
Each attribute, except special attributes like <code>if</code> and <code>forEach</code>, represents a value that shall be assigned to a property of a component after it is created. The attribute name is the property name, while the attribute value is the value to assign. For example, the following assigns <code>"Hello"</code> to window's title property. More precisely, <javadoc method="setTitle(java.lang.String)">org.zkoss.zul.Window</javadoc> will be called with <code>"Hello"</code>.
 
 
<source lang="xml">
 
<window title="Hello"/>
 
</source>
 
 
Like JSP, you could use EL as the value of any attribute. For example, the following assigns the value of the request parameter called name to window's title.
 
 
<source lang="xml">
 
<window title="${param.name}"/>
 
</source>
 
 
For more information about EL expressions, please refer to [[ZUML Reference/EL Expressions|ZUML Reference]].
 
 
===Assign Event Listener if Name Starts With <code>on</code>===
 
 
If the attribute name starts with <code>on</code> and the third letter is uppercase, an event listener is assigned. For example, we can register an event listener to handle the onClick event as follows.
 
 
<source lang="xml">
 
<button onClick="do_something_in_Java())"/>
 
</source>
 
 
The attribute value must be a valid Java code, and it will be interpreted<ref>ZK uses [http://www.beanshell.org BeanShell] to interpret it at run time</ref> when the event is received. You could specify different languages by prefixing the language name. For example, we could write the event listener in Groovy as follows.
 
 
<source lang="xml">
 
<vlayout onClick="groovy:self.appendChild(new Label('New'));">
 
Click me!
 
</vlayout>
 
</source>
 
 
<blockquote>
 
----
 
<references/>
 
</blockquote>
 
 
===Special Attributes===
 
 
There are a few special attributes dedicated to special functionality rather than assigning properties or handling events. For example, the forEach attribute is used to specify a collection of object such that the XML element it belongs will be evaluated repeatedly for each object of the collection.
 
 
<source lang="xml">
 
<listbox>
 
    <listitem forEach="${customers}" label="${each.name}"/>
 
</listbox>
 
</source>
 
 
For more information about special attributes, please refer to the [[ZK Developer's Reference/UI Composing/ZUML/forEach, if and unless|forEach, if and unless]] section, and [[ZUML Reference/ZUML/Attributes|ZUML Reference]]
 
 
==A XML Text Represents Label Component or Property's Value==
 
 
In general, a XML text is interpreted as a label component. For example,
 
 
<source lang="xml">
 
<window>
 
  Begin ${foo.whatever}
 
</window>
 
</source>
 
 
is equivalent to
 
 
<source lang="xml">
 
<window>
 
  <label value="Begin ${foo.whatever}"/>
 
</window>
 
</source>
 
 
===A XML Text as Property's Value===
 
 
Depending on component's implementation, the text nested in a XML element could be interpreted as the value of a component's particular property. For example, <javadoc>org.zkoss.zul.Html</javadoc> is one of this kind of components, and
 
 
<source lang="xml">
 
<html>Begin ${foo.whatever}</html>
 
</source>
 
 
is equivalent to
 
 
<source lang="xml">
 
<html content="Begin ${foo.whatever}"/>
 
</source>
 
 
It is designed to make it easy to specify multiple-line value, so it is usually used by particular components that requires the multiple-lines value. For a complete list of components that interprets the XML text as a property's value, please refer to [[ZUML Reference/ZUML/Texts|ZUML Reference]].
 
 
==A XML Processing Instruction Specifies the Page-wide Information==
 
 
Each XML processing instruction specifies the instruction how to process the XML document. It is called directives in ZK. For example, the following specifies the page title and style.
 
 
<source lang="xml">
 
<?page title="Grey background" style="background: grey"?>
 
</source>
 
 
Notice that there shall be ''no'' whitespace between the question mark and the processing instruction's name (i.e., page in the above example).
 
 
The other directives include the declaration of components, the class to initialize a page, the variable resolver for EL expressions, and so on.
 
For more information about directives, please refer to [[ZUML Reference/ZUML/Processing Instructions|ZUML Reference]].
 
  
 
=Language=
 
=Language=

Revision as of 06:53, 3 December 2010

Overview

There are two ways to compose UI: XML-based approach and pure-Java approach. Here we describe XML-based approach. For pure-Java approach, please refer the next chapter.

The declaration language is called ZK User Interface Markup Language (ZUML). It is based on XML. Each XML element instructs the ZK Loader which component to create. Each XML attribute describes what value to be assigned to the created component. Each XML processing instruction describes how to process the whole page, such as the page title. For example,

<?page title="Super Application"?>
<window title="Super Hello" border="normal">
    <button label="hi" onClick='alert("hi")'/>

where the first line specifies the page title, the second line creates a root component with title and border, and the third line creates a button with label and an event listener.

This chapter is about the general use of ZUML. For a complete reference, please refer to ZUML Reference.

Auto-completion with Schema

Many IDEs, such Eclipse, supports auto-completion if XML schema is specified as follows.

 <window xmlns="http://www.zkoss.org/2005/zul"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

In addition to downloading from http://www.zkoss.org/2005/zul/zul.xsd, you can find zul.xsd under the dist/xsd directory in the ZK binary distribution.



Language

A language (LanguageDefinition) is a collection of component definitions. It is also known as a component set.

For example, Window, Button and Combobox all belong to the same language called xul/html, which is also known as zul.

Component designers are free to designate a component definition to any component set they prefer, as long as there is no name conflict. In additions, it is OK to use components from several languages in the same ZUML page.

Language Identification

When parsing a ZUML document, ZK Loader have to decide the language that a XML element is associated, such that the correct component definition (ComponentDefinition) can be resolved. For example, in the following example, ZK needs to know window belongs to the xul/html language.

<window>

There are two ways to identify a language: filename's extension and XML namespace. Here are an overview of them. For more information please refer to ZUML Reference.

Identify by Filename's Extension

First, when a ZUML file is parsed, the language will be decided based on the extension of the filename or a URI (LanguageDefinition.getByExtension(String)). For example, the extensions associated with xul/html are zul and xul. Thus, if any file whose extension is zul or xul, the default language used to parse it will be assumed to be the xul/html language.

Another example: xhtml is another language (aka., a component set). It is associated with the extensions including zhtml, html, html, and xhtml. Thus, any file whose extension matches one of them will be considered as applying the zhtml language as default.

Notice that the association of filename's extension with a language is about how ZK Loader processes a ZUML page. To really have ZK Loader to process a file, you have to configure WEB-INF/web.xml correctly. For example, if you want to map all XUL files to ZK Loader, you could add the following to WEB-INF/web.xml:

<servlet-mapping>
	<servlet-name>zkLoader</servlet-name>
	<url-pattern>*.xul</url-pattern>
</servlet-mapping>

Identify by XML Namespace

If you map ZK Loader to a filename's extension that is not recognized, you could use XML namespaces to specify the language used to parse it. Each language is associated with a unique XML namespace that you can identify it easily in a ZUML document.

For example, you map ZK Loader to *.foo, then you could specify the xul/html language's namespace, which is http://www.zkoss.org/2005/zul:

<window xmlns="http://www.zkoss.org/2005/zul">
...

where the xmlns attribute declares a XML namespace to associate all element without explicit prefix, such as window in this case.

If you want to use several languages in the same XML document, you could use XML namespaces to distinguish them too. For example, the xhtml language's namespace is http://www.w3.org/1999/xhtml, and we could do as follows.

<window xmlns:h="http://www.w3.org/1999/xhtml">
    <h:table>
        <h:tr>
            <h:td>
                <button/>
            </h:td>
        </h:tr>
    </h:table>
</window>

Notice that table, tr and td are also components though they are very simple -- a simple wrapper of HTML tags. There is a better way to generate HTML tags: the native namespace. For more information please refer to the Native Namespace section.

Identify by XML Namespace with Shortcut

To make it easy to specify a namespace, you could specify a shortcut instead of the full namespace URI. For languages, the shortcut is the last word of the namespace URI. For example, zul for http://www.zkoss.org/2005/zul, and xhtml for http://www.w3.org/1999/xhtml. Thus, we can simply the previous example as follows.

<window xmlns:h="xhtml">
    <h:span>
        <h:tr>
            <h:td>
                <button/>
            </h:td>
        </h:tr>
    </h:table>
</window>

Version History

Last Update : 2010/12/3

Version Date Content
5.0.4 August, 2010 The shortcut was introduced to make it easy to specify a standard namespace, such as native, client and zk.
5.0.5 October, 2010 The shortcut was introduced to make it easy to specify a component set, such as zul and zhtml.



Last Update : 2010/12/03

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