-
FEATURED COMPONENTS
First time here? Check out the FAQ!
All,
I'm trying to format the date in a listcell, the value timeStamp is of type java.util.Date, I'm using org.zkoss.zkplus.databind.DateFormatConverter but can't quite see how to add the format annotation.
I've read the user guide and tried numerous permutations, none of which work, in most cases they simply reeturn the date using the convertor's default format, or throw an error.
I've tried something like this
<listcell value="@{console.timeStamp, converter='org.zkoss.zkplus.databind.DateFormatConverter'}" self="@{bind(format='dd-MMM-yyyy HH:mm z')}"/>
but this gives me the following error
org.zkoss.zk.ui.UiException: bean value must be defined as the first statement in the annotation <a:bind format=""/> for component <Listcell z_m4_q1>, id=z_m4_q1
I've only been using zk a few days now, so any help would be greatly appreciated, if anybody has got any links that might explain zk annotations and data binding, it would be greatly appreciated.
Regards
Tony
Hi,
Here is the binding wiki.
The correct syntax for the self-attribute is:
<label value="@{mydate, converter='org.zkoss.zkplus.datebind.DateFormatConverter' self="@{format(yyyy/MM/dd)}"/>
Check the self pattern! I found this example in the >> API <<.
And last: Here are some basic examples for databinding.
/Robert
Robert,
Thanks for the links, they we're quite useful.
I tried the sample self-attribute syntax, this is the same sample that I seen in the API docs & source code,
<label value="@{mydate, converter='org.zkoss.zkplus.datebind.DateFormatConverter' self="@{format(yyyy/MM/dd)}"/>
it's clearly not correct, it's not even correct XML, when I tried it I got the following error
-------------------
org.xml.sax.SAXParseException: Element type "label" must be followed by either attribute specifications, ">" or "/>".
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
javax.xml.parsers.SAXParser.parse(SAXParser.java:331)
org.zkoss.idom.input.SAXBuilder.build(SAXBuilder.java:300)
org.zkoss.zk.ui.metainfo.Parser.parse(Parser.java:102)
org.zkoss.zk.ui.metainfo.PageDefinitions$MyLoader.parse(PageDefinitions.java:196)
org.zkoss.web.util.resource.ResourceLoader.load(ResourceLoader.java:94)
org.zkoss.util.resource.ResourceCache$Info.load(ResourceCache.java:219)
org.zkoss.util.resource.ResourceCache$Info.<init>(ResourceCache.java:193)
org.zkoss.util.resource.ResourceCache.get(ResourceCache.java:134)
org.zkoss.web.util.resource.ResourceCaches.get(ResourceCaches.java:132)
org.zkoss.zk.ui.metainfo.PageDefinitions.getPageDefinition(PageDefinitions.java:142)
org.zkoss.zk.ui.impl.AbstractUiFactory.getPageDefinition(AbstractUiFactory.java:115)
org.zkoss.spring.bean.ZkSpringUiFactory.getPageDefinition(ZkSpringUiFactory.java:64)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:221)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:165)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
I aslo tried the following
<label value="@{console.timeStamp, converter='org.zkoss.zkplus.datebind.DateFormatConverter}'">
<custom-attributes format="yyyy/MMM/dd"/>
</label>
but that merely echoed the contents of the value attribute "@{console.timeStamp, converter='org.zkoss.zkplus.datebind.DateFormatConverter}".
Regards
Tony
Robert,
I've also tried the following syntax based on one of the linked examples, again no joy.
<listcell label="@{console.timeStamp, converter='org.zkoss.zkplus.datebind.DateFormatConverter}'">
<custom-attributes format="yyyy/MMM/dd"/>
</listcell>
I resorted to writing my own DateFormatConverter as I always want the date displayed in the same format. I've attached the code below.
Regards
Tony
/**
*
*/
package com.synergy.connect.controller.databind;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.zkoss.zk.ui.Component;
/**
* @author tonyd
*
*/
public class DateFormatConverter extends org.zkoss.zkplus.databind.DateFormatConverter {
/**
*
*/
public DateFormatConverter() {
}
/* (non-Javadoc)
* @see org.zkoss.zkplus.databind.DateFormatConverter#coerceToUi(java.lang.Object, org.zkoss.zk.ui.Component)
*/
@Override
public Object coerceToUi(Object val, Component comp) {
Date date = null;
if( val instanceof Timestamp ){
final Timestamp timestamp = (Timestamp)val;
date = new Date(timestamp.getTime());
} else if( val instanceof Date ){
date = (Date)val;
}
return date == null ? "" : (new SimpleDateFormat("dd-MMM-yyyy HH:mm 'GMT'")).format(date);
}
}
You have
<label value="@{mydate, converter='org.zkoss.zkplus.datebind.DateFormatConverter' self="@{format(yyyy/MM/dd)}"/>
<label value="@{mydate, converter=org.zkoss.zkplus.datebind.DateFormatConverter}" self="@{format(yyyy/MM/dd)}"/>
Hi audioworm,
your are right, the API example is wrong, there are 2 faults in this example.
Fault 1: binding for value is unclosed (as xmedeko wrote)
Fault 2: the binding class is wrong is uses package ...datebind... instead of the correct databind.
Here is working example:
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./mywin"?> <window id="mywin" border="none"> ZK name check list : <zscript> Date mydate = new Date(); </zscript> <label value="@{mydate, converter='org.zkoss.zkplus.databind.DateFormatConverter'}" self="@{format(yyyy/MM/dd)}"/> </window>
/Robert
@zkteam: could anybody fix the api docs for the DateFormatConverter?
Asked: 2010-06-29 08:01:05 +0800
Seen: 1,344 times
Last updated: Oct 28 '10