-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I want to use Databinding with a Datebox. But the value in the database could be empty (null). But binding a null-Value to a Datebox runs into Exception because a null-Value ist converted to a ""-String and this String could not be converted to a Date. Is there any workaround?
ZK 5.0.4
You could use a converter to handle this, here's a converter sample.
package j1t0gtrk$v1;import java.util.*;
public class Person{
private String firstName;
private String lastName;
private Date birthday;public Person(){
}
public Person(String first,String last,Date bir){
this.firstName = first;
this.lastName = last;
this.birthday = bir;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return this.firstName;
}public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return this.lastName;
}
public void setBirthday(Date bir){
this.birthday = bir;
}
public Date getBirthday(){
return this.birthday;
}
}
package j1t0gtrk$v1;import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Listcell;
import java.text.SimpleDateFormat;
import java.util.Date;public class MyDateTypeConverter implements TypeConverter {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
public Object coerceToBean(java.lang.Object val,
org.zkoss.zk.ui.Component comp) {
return null;
}
public Object coerceToUi(java.lang.Object val,
org.zkoss.zk.ui.Component comp) {
return sdf.format((Date) val);
}
}
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<window width="500px">
<zscript><![CDATA[
import j1t0gtrk$v1.Person;
//prepare the example persons List
List persons = new ArrayList();
persons.add(new Person("Tony", "Wang", new Date(86,11,23)));
persons.add(new Person("Tracy", "Kao", new Date(85,10,20)));
persons.add(new Person("Jone", "Lee", new Date(80,10,20)));
]]>
</zscript>
<listbox rows="4" model="@{persons}">
<listhead>
<listheader label="First Name" width="100px" />
<listheader label="Last Name" width="100px" />
<listheader label="Married" width="100px" />
</listhead>
<listitem self="@{each=person}">
<listcell label="@{person.firstName}" />
<listcell label="@{person.lastName}" />
<listcell
label="@{person.birthday, converter='j1t0gtrk$v1.MyDateTypeConverter'}" />
</listitem>
</listbox>
</window>
Problem is, what should sdf.format((Date) val) return for a null Value? Normally Exception. If the TypeConverter returns null, Databinding makes it to ""-String and Databinding runs into Exception.
public Object coerceToUi(java.lang.Object val,org.zkoss.zk.ui.Component comp) {
return sdf.format((Date) val);
}
I have a little bit modified Tonys sample for null valued dates.
package j3m89vri$v3;import java.util.*;
public class Person{
private String firstName;
private String lastName;
private Date birthday;public Person(){
}
public Person(String first,String last,Date bir){
this.firstName = first;
this.lastName = last;
this.birthday = bir;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return this.firstName;
}public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return this.lastName;
}
public void setBirthday(Date bir){
this.birthday = bir;
}
public Date getBirthday(){
return this.birthday;
}
}
package j3m89vri$v3;import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Listcell;
import java.text.SimpleDateFormat;
import java.util.Date;public class MyDateTypeConverter implements TypeConverter {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
public Object coerceToBean(java.lang.Object val,
org.zkoss.zk.ui.Component comp) {
return null;
}
public Object coerceToUi(java.lang.Object val,
org.zkoss.zk.ui.Component comp) {if (val == null)
return "";
else
return sdf.format((Date) val);
}
}
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<window width="500px">
<zscript><![CDATA[
import j3m89vri$v3.Person;
//prepare the example persons List
List persons = new ArrayList();
persons.add(new Person("Tony", "Wang", new Date(86,11,23)));
persons.add(new Person("Tracy", "Kao", new Date(85,10,20)));
persons.add(new Person("Jone", "Lee", null ));
]]>
</zscript>
<listbox rows="4" model="@{persons}">
<listhead>
<listheader label="First Name" width="100px" />
<listheader label="Last Name" width="100px" />
<listheader label="Married" width="100px" />
</listhead>
<listitem self="@{each=person}">
<listcell label="@{person.firstName}" />
<listcell label="@{person.lastName}" />
<listcell
label="@{person.birthday, converter='j3m89vri$v3.MyDateTypeConverter'}" />
</listitem>
</listbox>
</window>
best
Stephan
PS: TonyQ, ZKFiddle is great work!
Asked: 2011-09-21 09:06:20 +0800
Seen: 227 times
Last updated: Sep 22 '11