0

Databinding with Datebox and Date is null

asked 2011-09-21 09:06:20 +0800

pweller gravatar image pweller
41 1

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

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2011-09-21 14:32:55 +0800

TonyQ gravatar image TonyQ
642
https://www.masterbranch....

You could use a converter to handle this, here's a converter sample.

ZKFiddle-Link

Person.java
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;
}

}


MyDateTypeConverter.java
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);
}
}


index.zul
<?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>

link publish delete flag offensive edit

answered 2011-09-22 01:45:03 +0800

pweller gravatar image pweller
41 1

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);
}

link publish delete flag offensive edit

answered 2011-09-22 03:57:31 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-09-22 03:58:59 +0800

I have a little bit modified Tonys sample for null valued dates.

ZKFiddle-Link

Person.java
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;
}

}


MyDateTypeConverter.java
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);
}
}


index.zul
<?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!

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-09-21 09:06:20 +0800

Seen: 227 times

Last updated: Sep 22 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More