1
ZK 範例 / ZK Data Binding 資料綑綁
« on: July 21, 2012, 11:56:59 PM »
ZK Data Binding 把Person Object 和ZUL 上 Component 綑綁在一起,使輸入的資料同步對應到各 Fields 中
Sample05.zul
-------------------
Sample05.java
--------------------
Person.java
------------------
Sample05.zul
-------------------
Code: [Select]
<?page title="Data Binding" contentType="text/html;charset=UTF-8"?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
<zk>
<window id="win" title="ZK Data Binding SAMPLE05" width="600px" border="normal" closable="true" sizable="true" apply="inisoft.mo.gui.Sample05">
<grid>
<columns sizable="true">
<column label="Type" sort="auto" width="200px"/><column label="Content" width="360px"/>
</columns>
<rows>
<row>
<label value="First Name :"/><textbox id="txt_firstname" width="200px"/>
</row>
<row>
<label value="Last Name :"/><textbox id="txt_lastname" width="200px"/>
</row>
<row>
<label value="Tel :"/><textbox id="txt_tel" width="200px"/>
</row>
<row>
<label value="Birthday :"/><datebox id="dat_birthday" width="150px"/>
</row>
<row>
<label value=""/><button id="btn_Submit" label="Submit" width="150px"/>
</row>
</rows>
</grid>
<separator/>
<vbox>
<label value="Result : "/>
<label id="lbl_info"/>
</vbox>
</window>
</zk>
Sample05.java
--------------------
Code: [Select]
package inisoft.mo.gui;
import inisoft.mo.value.object.Person;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.*;
/**
* @author Van Chan
*/
public class Sample05 extends GenericForwardComposer{
Window win;
Textbox txt_firstname,txt_lastname,txt_tel;
Datebox dat_birthday;
Label lbl_info;
Button btn_Submit;
private AnnotateDataBinder binder;
private Person person;
@Override
public void doAfterCompose(Component comp)throws Exception
{
super.doAfterCompose(comp);
binder = new AnnotateDataBinder(win);
person = new Person("Van","Chan","1234567",new Date("2000/1/1"));
bindObject(person);
}
public void bindObject(Object o)
{
binder.bindBean("person",o);
binder.addBinding(txt_firstname, "value", "person.FirstName");
binder.addBinding(txt_lastname, "value", "person.LastName");
binder.addBinding(txt_tel, "value", "person.Tel");
binder.addBinding(dat_birthday, "value", "person.Birthday");
binder.loadAll();
}
public void onClick$btn_Submit(Event event)throws Exception{
Locale locale = Locale.ENGLISH;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy",locale);
String birthday=dateFormat.format(person.getBirthday());
String details=person.getFirstName()+" / "+person.getLastName()+" / "+person.getTel()+" / "+birthday;
lbl_info.setValue(details);
}
}
Person.java
------------------
Code: [Select]
package inisoft.mo.value.object;
import java.util.Date;
/**
* @author Van Chan
*/
public class Person implements java.io.Serializable{
private Integer personId;
private String firstName;
private String lastName;
private String tel;
private Date birthday;
public Person() {
}
public Person(String firstName, String lastName, String tel, Date birthday) {
this.firstName = firstName;
this.lastName = lastName;
this.tel = tel;
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}