0

How to use org.zkoss.zkplus.databind.DateFormatConverter ?

asked 2010-06-29 08:01:05 +0800

audioworm gravatar image audioworm
36 1

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

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2010-06-29 10:12:50 +0800

robertpic71 gravatar image robertpic71
1275 1

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

link publish delete flag offensive edit

answered 2010-06-30 02:55:28 +0800

audioworm gravatar image audioworm
36 1

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

link publish delete flag offensive edit

answered 2010-06-30 02:57:36 +0800

audioworm gravatar image audioworm
36 1

Robert,

The API docs for the org.zkoss.zkplus.databind.DateFormatConverter need to be corrected.

Regards
Tony

link publish delete flag offensive edit

answered 2010-06-30 03:04:07 +0800

audioworm gravatar image audioworm
36 1

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

}

link publish delete flag offensive edit

answered 2010-06-30 04:01:55 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

You have

<label value="@{mydate, converter='org.zkoss.zkplus.datebind.DateFormatConverter' self="@{format(yyyy/MM/dd)}"/>

try
<label value="@{mydate, converter=org.zkoss.zkplus.datebind.DateFormatConverter}" self="@{format(yyyy/MM/dd)}"/>

now it should be a correct XML

link publish delete flag offensive edit

answered 2010-06-30 04:46:25 +0800

robertpic71 gravatar image robertpic71
1275 1

updated 2010-06-30 04:47:25 +0800

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?

link publish delete flag offensive edit

answered 2010-06-30 06:10:19 +0800

SimonPai gravatar image SimonPai
1696 1

Hi all,

Thanks for pointing this out. I have add this to our bug tracker.
The API Javadoc is built with source code, so it will be fixed with our next Freshly Build.

Thanks,
Simon

link publish delete flag offensive edit

answered 2010-06-30 07:23:31 +0800

audioworm gravatar image audioworm
36 1

All,

Thanks very much for all your help, the problem has now been resolved.

Regards
Tony

link publish delete flag offensive edit

answered 2010-10-28 18:14:15 +0800

javahtml gravatar image javahtml flag of Colombia
7

Spam Blocked

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: 2010-06-29 08:01:05 +0800

Seen: 1,344 times

Last updated: Oct 28 '10

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