Load ZUML in Java

From Documentation
Revision as of 08:07, 9 November 2010 by Tomyeh (talk | contribs)



Execution provides a collection of methods to allow you to create components based a ZUML document, such as Execution.createComponents(String, Component, Map), Execution.createComponentsDirectly(String, String, Component, Map) and many others. In additions, Executions provides a similar collection of shortcuts, so that you don't have to retrieve the current execution first.

For example,

public class Controller extends GenericForwardComposer {
    private Window main; //assumed wired automatically
    public void onClick() {
        Executions.createComponentsDirectly(
           "<listbox><listitem label=\"foo\"/></listbox>", "zul", this, null);
    }
...

Create from an URI

There are several ways to create components based a ZUML document. One of the most common approach is to create components from a URI.

Map arg = new HashMap();
arg.put("some.param", someValue);
Executions.createComponents("/foo/my.zul", parent, arg); //attach to page no matter parent is null or not

where parent (an instance of Component) will become the parent of the components specified in the ZUML document. If parent is null, the components specified in the ZUML documents will become the root components of the current page. In other words, the components created by Execution.createComponents(String, Component, Map) will be attached the current page.

Create Components Not Attached to Any Page

If you want to create components that won't be attached to a page, you could use Execution.createComponents(String, Map). It is useful if you wan to maintain a cache of components or implement a utility. For example,

Map arg = new HashMap();
arg.put("some.param", someValue);
Component[] comps = Executions.getCurrent().createComponents("/foo/my.zul", arg); //won't be attached to a page
cache.put("pool", comps); //you can store and use them later since they are attached to any page

Create Components in Working Thread

With Executions.createComponents(WebApp, String, Map), you could create components in a working thread without any execution[1], though it is rare.

Of course, the components being created by Executions.createComponents(WebApp, String, Map) won't be attached to any page. You have to attach them manually, if you want to show them to the client.


  1. It means Executions.getCurrent() returns null. For example, it happens when the application starts, or in a working thread.

Create from Content Directly

If the ZUML document is a resource of Web application (i.e., not accessible thru ServletContext), you could use one of createComponentsDirectly methods. For example, you could read the content into a string from database and pass it to Execution.createComponentsDirectly(String, String, Component, Map). Or, you could represent the content as a reader (say, representing BLOB in database) and then pass it to Execution.createComponentsDirectly(Reader, String, Component, Map)

Create from Page Definition

When creating components from URI (such as Execution.createComponents(String, Component, Map)), ZK Loader will cache the parsed result and reuse it to speed up the rendering.

However, if you create components from content directly (such as Execution.createComponentsDirectly(String, String, Component, Map)), there is no way to cache the parsed result. In other words, the ZUML content will be parsed each time createComponentsDirectly is called.

It is OK if the invocation does not happen frequently. However, if you want to improve the performance, you could parse the content into PageDefinition by use of Executions.getPageDefinitionDirectly(WebApp, String, String), cache it, and then invoke Executions.createComponents(PageDefinition, Component, Map) to create them.

Version History

Last Update : 2010/11/9

Version Date Content
     



Last Update : 2010/11/09

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