-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello to all,
i want to develop one simple application in zk.in which user will enter name in text box and select languages he knows in listbox with select mold.My model for languages will be from database.I want to do this things with databinding so that i can use bean for insert , update and delete operation.I had used data binding with text box , but not knowing binding of listbox and then using binded bean to perform database operations.So help me to manage this application with databinding.
Hi Ashish,
I am not having sample code ,but i want to do such kind of thing in zk with data binding.so could u suggest me the simple way to achieve this thing??or can u give me any reference from where i can understand data binding of listbox with select mold??plz give ur valuable feedbacks.
Thanks
-Rajesh
RobertPic has a nice sample site with codes here
Thanks for your help terrytornado,
But my problem does not resolved.i am posting my sample code over here.in which i am having add customer form with text box select box with data binding to Customer pojo.
my list box with select mold is list LanguagePojo bean and on clicking add button i want to print values of Text box and Select Box using binding..
Codes.............
==============
addCustomer.zul
==============
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="addCustomer" ?>
<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
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">
<window id="addCustomer" apply="org.zkoss.zkdemo.userguide.addCustomerComposer"
border="none" width="500px" height="300px" title="Add Customer" >
<zscript>
</zscript>
<grid width="400px">
<rows>
<row> Name:
<textbox id="txtName" value="@{controller.customer.name}" />
</row>
<row>Language :
<listbox id="lstLang" mold="select" rows="1"
model="@{controller.languages}" selectedItem="@{controller.customer.codeId}">
<listitem self="@{each=lang}" label="@{lang.desc}" value="@{lang.codeId}"/>
</listbox>
</row>
<row><button label="Add" id="stnSubmit"/></row>
</rows>
</grid>
</window>
</zk>
==============
addCustomerComposer.java
==============
package org.zkoss.zkdemo.userguide;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import com.dylt.dto.customer.CustomerBondAgentBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Window;
/**
*
* @author root
*/
public class addCustomerComposer extends GenericForwardComposer {
protected transient Window addCustomer; // autowired
protected transient AnnotateDataBinder binder;
private CustomerPojo customer;
private List languages;
public void onCreate$addCustomer(Event event) throws Exception {
addCustomer.setVariable("controller", this, false);
binder = new AnnotateDataBinder(addCustomer);
binder.loadAll();
setCustomer(new CustomerPojo());
languages = new ArrayList();
for (int i = 1; i <= 3; i++) {
LanguagePojo objLang = new LanguagePojo(i,"Language"+i);
languages.add(objLang);
}
setLanguages(languages);
binder.loadAll();
}
public List getLanguages() {
return languages;
}
public void setLanguages(List languages) {
this.languages = languages;
}
public CustomerPojo getCustomer() {
return customer;
}
public void setCustomer(CustomerPojo customer) {
this.customer = customer;
}
public void onClick$stnSubmit(Event event) throws Exception{
CustomerPojo customer = getCustomer();
System.out.println("###Name#####"+customer.getName());
System.out.println("###Id#####"+customer.getCodeId());
}
}
==================
LanguagePojo.java
==================
package org.zkoss.zkdemo.userguide;
public class LanguagePojo {
private int codeId;
private String desc;
public LanguagePojo (int id,String description){
setCodeId(id);
setDesc(description);
}
public int getCodeId() {
return codeId;
}
public void setCodeId(int codeId) {
this.codeId = codeId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
==============
CustomerPojo.java
==============
package org.zkoss.zkdemo.userguide;
public class CustomerPojo {
private String name;
private int codeId;
public int getCodeId() {
return codeId;
}
public void setCodeId(int codeId) {
this.codeId = codeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
=============================================================
i want to set Name and Code id of Language to the CustomerPojo bean so that,bean can be passed for further database operation.
but on changing Language combo box i am getting below error.....
ERROR Mar 12, 2010 12:12:14 PM org.zkoss.zk.ui.impl.UiEngineImpl handleError:1128
SEVERE: >>org.zkoss.zk.ui.UiException: java.lang.NoSuchMethodException: class org.zkoss.zkdemo.userguide.CustomerPojo: name=codeId args=[class org.zkoss.zkdemo.userguide.LanguagePojo]
>>java.lang.NoSuchMethodException: class org.zkoss.zkdemo.userguide.CustomerPojo: name=codeId args=[class org.zkoss.zkdemo.userguide.LanguagePojo]
>> at org.zkoss.lang.Classes.myGetAcsObj(Classes.java:954)
>> at org.zkoss.lang.Classes.getAccessibleObject(Classes.java:882)
>> at org.zkoss.lang.reflect.Fields.set(Fields.java:138)
>> at org.zkoss.zkplus.databind.DataBinder.setBeanAndRegisterBeanSameNodes(DataBinder.java:1081)
>> at org.zkoss.zkplus.databind.Binding.saveAttributeValue(Binding.java:423)
>> at org.zkoss.zkplus.databind.Binding.access$100(Binding.java:48)
========================================
so please help me in this case..i am just testing binding in zk.
is there anything missing?? or any other issue??
please reply me.
Hi rsavaliya,
do you use Hibernate/JPA "under the hood"?
Databinding works perfect for full definied bean relations, in your case:
The CustomerPojo do not hold the language/code-Id, but the language/code itself.
i.e.
==============
CustomerPojo.java
==============
package org.zkoss.zkdemo.userguide;
public class CustomerPojo {
private String name;
private LanguagePojo language;
..
the listbox looks lie:
<listbox id="lstLang" mold="select" rows="1"
model="@{controller.languages}" selectedItem="@{controller.customer.language}">
The databinder could follow the object, i.e. see an example:
...
<listitem self="@{each=customer}">
<listcell label="@{customer.name}" />
<listcell label="@{customer.language.desc}" />
...
The other way is to use an own Typeconverter for your case. Ask again if you don't want use beans with full defined relations.
/Robert
thanx robert,
but as my bean classes are predefined,i cant change them to full fill my requirement in zk.so would you please suggest me any other simple way for given example ?? please suggest me in detail as i am not aware of zk in detail...thanx in advance..
/rajesh
Hi Rajesh,
I second robert's approach as currently there is no way to connect CustomerPojo with LanguagePojo. Have you read this section of databinding. This might be the missing link in your implementation.
Thanks,
Ashish
Asked: 2010-03-10 06:30:28 +0800
Seen: 1,057 times
Last updated: Mar 15 '10