-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hiho all,
i'm heavily learning ZK. Most time it makes fun. At time i'm figthing with the implementation from my spring/hibernate backend (services/dao/model) from an other project.
Can someonye explain me (in easy words, i.e. with example) what is the difference between use="" and apply="". At time i have problems to access my spring controlled beans in a .zul file.
Sometimes i have only access to methods by using the use="org.myfirm.sampleapp.Class". But with the full qualified packagename it goes around spring, i mean.
Thanks for everybody
Stephan
The use, means you want to reimplement the component.
For example, if you write:
<window use="myWinClass"> </window>
Then, when the zul component 'window' of this page is created, an instance of myWinClass will be created instead of a Window.
Obviously, myWinClass should extend Window.
If you use the 'apply', you are just linking a Composer to your component.
In my opinion, you'll need a Composer instead of the 'use' in the 99% of the cases.
There is a beautiful documentation about composers at : http://www.zkoss.org/smalltalks/mvc3/
@ziccardi,
many thanks.
I will read the article today.
Stephan
Yes, this article was helpful.
I changed my code for a button event like the example class with extends Window implements AfterCompose.
But.. this doesn't work for follow szenario. I have a Listbox with records of customers and on the end of each line there is a button for calling the customers dialog with the selected cutomer record. In that case i cannot give the buttons an id because they are created dynamically for each customers record.
>> But.. this doesn't work for follow szenario. I have a Listbox with records of customers and on the end of each line there is a button for calling the customers
>> dialog with the selected cutomer record. In that case i cannot give the buttons an id because they are created dynamically for each customers record.
How do create you Listbox?
forEach, model + Render, pure Java, @databinding
However the fowardutil is pretty smart and handels templates from forEach and @databinding. I.e. the following code works:
<rows id="tableGridRows"> <row self="@{each=entry}"> <intbox value="@{entry.mfifir}" cols="2" maxlength="2" format="##" tooltip="help"/> <label value="@{entry.mfifirkbez}" tooltip="help"/> ... <image id="trash" src="images/trash empty_24.png" hover="images/trash full_24.png"/> </row> </rows> in my Javaclass (could be ext. Window or Composer) public void onClick$trash(Event event) { // handels onClick from trashicon
However if you fill your listbox without zul-definition in java it should work with button.addForward
i.e.
button.addForward("onClick", win, "onClickTrash");
public void onClickTrash(Event event) { // handels onClick from trashbutton
Don't use the $ in you manuals Eventhandlers.
Testet with a normal button (i have no lists without databinding here).
/Robert
The solution depends on how you implemented your code.
I suppose you use a model and a renderer to populate your list.
To solve your problem, you can use the following method.
Inside the renderer:
public void render(Listitem item, Object obj) { // Create your row // ... // Now it's time to create the button: Button btn = new Button(); // Configure your button with your label, etc... // Now that your button is ready, add this (I save the data as button attribute): btn.addAttribute("CUSTOMERDATA", obj); ComponentsCtrl.applyForward(btn, "onClick=onButtonHasClickedInList"); }
Now, inside the composer:
// ... // Your composer code // ... // Here is the method that will handle the clicks on the list buttons: public void onButtonHasClickedInList(Event evt) { // The event is a ForwardEvent... ForwardEvent fe = (ForwardEvent) evt; // Getting the original Event Event evt = fe.getOrigin(); // Getting the component that triggered the original event (i.e. the button) Button btn = (Button) evt.getTarget(); // Getting the Customer Data (it can be any type. for the example, I'll use Object).. // Each button will have it's own customer data instance Object obj = btn.getAttribute("CUSTOMERDATA"); // Do what you want with the customer data }
@zicardi
@robertpic
thanks for the answering. At time technically i cannot copy the code here in. i will do it on evening.
I have several problems/errors at the same time
1. I work with spring managed beans.
2. I don't know how to call a spring managed bean methode from a <zscript> </zcsipt>
to get my Dialog in a hard way for testing the composer events. When i do it with new Bean() it crashes on the next time i probe the composer event.
3. yet my listbox looks like a jsf listbox. so the listItems are filled with the foreach=${customerListBean.allCustomer}". The buttons are created on same way.
I cannot test these button events in composer for each row because i cannot give them an id. By doing this after the second row i became an error for duplicate button ids.
I will post the zul-file this evening. and i know this is the false way. but it's my first try. I hope
you can put it in a right way. :-)
Stephan
Hi Stephan.
>I cannot test these button events in composer for each row because
>i cannot give them an id. By doing this after
>the second row i became >an error for duplicate button ids.
If you use the method I told before (the forward), you don't need to give the button an id.
An example could be (I didn't check, so maybe you'll find some typo):
<zk> <zscript> class comp extends org.zkoss.zk.ui.util.GenericForwardComposer { public void onButtonClicked(Event evt) { ForwardEvent fe = (ForwardEvent) evt; Event e = fe.getOrigin(); Button btn = (Button) e.getTarget(); alert ("LIST ITEM DATA : " + btn.getAttribute("DATA")); } } </zscript> <window title="Listbox" border="normal" use="comp"> <vbox> <listbox width="250px" id="myList"> <listhead sizable="true"> <listheader label="Name" sort="auto"/> <listheader label="Action"/> </listhead> </listbox> <zscript> <![CDATA[ List data = new ArrayList(); for (int i = 0; i < 2; ) data.add("ITEM : " + ++i); myList.setModel(new ListModelList(data)); myList.setItemRenderer( new ListitemRenderer() { void render(Listitem item, java.lang.Object data) { Listcell cell = new Listcell(); Label lbl = new Label(data.toString()); lbl.setParent(cell); cell.setParent(item); Listcell cell = new Listcell(); Button btn = new Button(); btn.setLabel("Click me"); btn.setParent(cell); btn.setAttribute("DATA", data); cell.setParent(item); ComponentsCtrl.applyForward(btn, "onClick=onButtonClicked"); } } ); ]]> </zscript> </vbox> </window> </zk>
@ziccardi
i will jump from the house. :-((
I cannot test it. I have seen in the mvc tutorial that others have use and apply with the '$' for spring beans. Why does this not works for me :-((((((((((((((
ZK error: java.lang.ClassCastException: org.myfirm.bean.KundeListBean$$EnhancerByCGLIB$$3b267775 cannot be cast to java.lang.String.
AArgh, all times i have errors that seems that i'm the only with it.
Hi Stephan.
The
>ZK error: java.lang.ClassCastException: org.myfirm.bean.KundeListBean$$EnhancerByCGLIB$$3b267775 cannot be cast to java.lang.String.
means that you are trying to cast an CGLIB enhanched class (KundeListBean) to a string.
Where the error happens, try to cast it to a KundeListBean instead than String...
Hope this helps.
>> I cannot test it. I have seen in the mvc tutorial that others have use and apply with the '$' for spring
>> beans. Why does this not works for me :-((((((((((((((
I see no variable-resolver in your posted zul-file.
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
/Robert
Asked: 2008-11-19 09:23:00 +0800
Seen: 5,776 times
Last updated: Jan 18 '11