-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, I'd like to use the new "apply" attribute in the ZUL over the older "use" attribute but don't quite know how to convert the old Java code to the newer one. Attached is a quick sample. Could someone help with switching this code to be used with "apply" ? Thanks very much..
ZUL file:
======
<?xml version="1.0" encoding="UTF-8"?> <zk xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.zkoss.org/2005/zul' xmlns:h='http://www.w3.org/1999/xhtml' xsi:schemaLocation='http://www.zkoss.org/2005/zul WEB-INF/xsd/zul.xsd' > <window id='testWin' use='GcmScLaunch.ControllerTestWin' border='normal' title='A Test Page'> <listbox id='testLstBox' mold="select" rows="1" onCreate='testWin.testLstBox_onCreate()' > </listbox> <button id='qryShowAllSalesGroupBtn' label='Show Replies' autodisable='self' style='width:18ex;' onClick='testWin.qryShowAllSalesGroupBtn_onClick()' /> </window> </zk>
package GcmScLaunch; import java.sql.SQLException; import javax.servlet.ServletContext; import java.util.*; import java.io.*; import org.zkoss.zul.*; import org.zkoss.zk.ui.*; import org.zkoss.zk.ui.ext.*; import org.zkoss.zk.ui.event.*; import org.zkoss.zk.ui.util.*; /** Controller class for test.zul page */ public class ControllerTestWin extends Window { private Listbox getTestLstBox() { return ((Listbox) getFellow("testLstBox")); }//end method /** Controller constructor */ public ControllerTestWin() { }//end contructor public void testLstBox_onCreate() throws Exception { Listbox testLstBox = this.getTestLstBox(); (new Listitem("Hello World!!")).setParent(testLstBox); testLstBox.setSelectedIndex(0); }//end method public void qryShowAllSalesGroupBtn_onClick() throws Exception { Listbox testLstBox = this.getTestLstBox(); testLstBox.getChildren().clear(); testLstBox.invalidate(); String theItem = "Hi From World"; for(int i=0; i <= 10; i++) { String item = theItem + String.valueOf(i); (new Listitem(item)).setParent(testLstBox); testLstBox.setSelectedIndex(0); }//end for }//end method }//end class
HI
zul:
<?xml version="1.0" encoding="UTF-8"?> <zk> <window id='testWin' apply='GcmScLaunch.TestWinViewCtrl' border='normal' title='A Test Page'> <listbox id='testLstBox' mold="select" rows="1"></listbox> <button id='qryShowAllSalesGroupBtn' label='Show Replies' autodisable='self' style='width:18ex;' ></button> </window> </zk>
composer:
package GcmScLaunch; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.Button; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listitem; import org.zkoss.zul.Window; public class TestWinViewCtrl extends GenericForwardComposer { //private Window testWin; //private Button qryShowAllSalesGroupBtn; //private Listbox testLstBox; @Override public void doAfterCompose(Component comp)throws Exception{ super.doAfterCompose(comp); } public void onClick$qryShowAllSalesGroupBtn(Event evt){ // Listbox testLstBox = (Listbox)evt.getTarget().getFellow("testLstBox"); testLstBox.getChildren().clear(); testLstBox.invalidate(); String theItem = "Hi From World"; for(int i=0; i <= 10; i++) { String item = theItem + String.valueOf(i); (new Listitem(item)).setParent(testLstBox); testLstBox.setSelectedIndex(0); }//end for } public void onCreate$testLstBox(Event evt){ // Listbox testLstBox = (Listbox)evt.getTarget().getFellow("testLstBox"); (new Listitem("Hello World!!")).setParent(testLstBox); testLstBox.setSelectedIndex(0); } }
For more infomation please refer to the follows:
http://wiki.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer
you can do that in the onCreate method of the window
public class TestWinViewCtrl extends GenericForwardComposer { . . . private Window testWin; private Button qryShowAllSalesGroupBtn; private Listbox testLstBox; . . public void onCreate$testWin(Event evt){ (new Listitem("Hello World!!")).setParent(testLstBox); testLstBox.setSelectedIndex(0); }
best
Stephan
Thanks Stephan. Now it works! But why can't onCreate() of the list box be called directly like the old code where the class extends Window instead of GenericForwardComposer? Also, with this new Composer class, if I want to initialize other private variables like DAO connection, Sessions, etc. should I do them inside the constructor method or inside that doAfterCompose() method ?
//private Listbox testLstBox;
--> //
By using the GenericForwardComposer zk goes through all instance vars of the declared components to have access by naming convention. You have comment out the 'private Listbox testLstBox;' code line so the zk parser cannot see them.
best
Stephan
PS: There are a few sample applications from the contributors and there are several ways to come to goal. Look like they do such things. A good starting point for a deeper look inside such a complex app is here: http://www.zk-web.de/zksample2/
with code change history and free source codes. Thread is here.
Asked: 2011-12-20 22:35:09 +0800
Seen: 424 times
Last updated: Dec 21 '11