The use Attribute
Embedding code improperly in pages might cause a maintenance headache, so it's best to separate control code from the code for the UI. There are two ways to separate code from views.
First, you could put these control codes in a separate Java class, and then register event listeners to invoke the proper methods accordingly. For example, you could invoke your methods to initialize or to cancel a process by registering the onCreate(), onOK(), and onCancelQ event listeners to invoke those methods defined in MyClass class:
<window id="main" onCreate="MyClass.init(main)" onOK="MyClass.save(main)" onCancel="MyClass.cancel(main)"/>
For this to work, you must have a Java class called MyClass as shown in Listing 4-5.
Listing 4-5. MyClass.java import org.zkoss.zul.Window; public class MyClass { public static void init(Window main) { //does initialization
public static void save(Window main) { //saves the result
public static void cancel(Window main) { //cancel any changes
The other option for separating the view code is to assign the use attribute to specify a class to replace the default component class:
<window use="MyWindow"/>
In this case, you must have a Java class called MyWindow, as shown in Listing 4-6.
Listing 4-6. MyWindow.java import org.zkoss.zul.Window; public class MyWindow extends Window { public void onCreate() { //does initialization
public void onOK() { //save the result
public void onCancel() { //cancel any changes
Post a comment