-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello
I'm walking through the examples and the hibernate example fails, if I use the included zscript code.
<listitem forEach="${allEvents}" value="${each}">
<listcell label="${each.title}"/>
<zscript>String datestr = new SimpleDateFormat("yyyy/MM/dd").format(each.date);</zscript>
<listcell label="${datestr}"/>
</listitem>
org.zkoss.zk.ui.UiException: Sourced file: inline evaluation of: `` String datestr = new SimpleDateFormat("yyyy/MM/dd").format(eac . . . '' : Typed variable declaration : Class or variable not found: each.date : at Line: 19 : in file: inline evaluation of: `` String datestr = new SimpleDateFormat("yyyy/MM/dd").format(eac . . . '' : each .date
If I simply use <listcell label="${each.date}"/> and delete the <zscript/> it works fine, it looks to be calling the toString() method.
So did the scoping rules change, or is something needed to tell <zscript> about the ${each} variable?
Thanks,
Eric
It's not a scoping issue, but a phase issue. Zscript is evaluated when it's parsed, so it doesn't know about "each" yet.
Hi Cary, please can you write me, because of your email address. I doesn't find it on the triples site :-)
sge(at)forsthaus(dot)de
Please, show us your complete code.
Okay, this came directly from the example, from the link above, was it written for a old version of zk?
<?page title="event"?>
<zk>
<zscript><![CDATA[
import java.util.Date;
import java.text.SimpleDateFormat;
import org.app.Event;
import org.app.EventDao;
//fetch all allEvents from database
List allEvents = new EventDao().findAll();
]]></zscript>
<listbox id="lbxEvents">
<listhead>
<listheader label="Title" width="200px"/>
<listheader label="Date" width="100px"/>
</listhead>
<listitem forEach="${allEvents}" value="${each}">
<listcell label="${each.title}"/>
<zscript>String datestr = new SimpleDateFormat("yyyy/MM/dd").format(each.date);</zscript>
<listcell label="${datestr}"/>
</listitem>
</listbox>
</zk>
Thanks,
Eric
do you have the other stuff like the EventDAO (for the demo data) implemented too ?
Yes I have everything implemented, I'm using zk 5.0.6
If I delete this line;
<zscript>String datestr = new SimpleDateFormat("yyyy/MM/dd").format(each.date);</zscript>
and change this line;
<listcell label="${datestr}"/>
to;
<listcell label="${each.date}"/>
everything works, it's simply calling each.date.toString()
I'm trying to figure out why the example is broken and how to fix it.
caclark say; It's not a scoping issue, but a phase issue. Zscript is evaluated when it's parsed, so it doesn't know about "each" yet.
So how do you rewrite the example so others don't have the same problem? I think it's a very common problem to need to format a date object on the view.
Thanks,
Eric
Hello
Just so others don't run into the same problem when learning ZK can you update the example at;
http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/Integration/Hibernate
replace
<?page title="event"?> <zk> <zscript><![CDATA[ import java.util.Date; import java.text.SimpleDateFormat; import events.Event; import events.EventDao; //fetch all allEvents from database List allEvents = new EventDao().findAll(); ]]></zscript> <listbox id="lbxEvents"> <listhead> <listheader label="Title" width="200px"/> <listheader label="Date" width="100px"/> </listhead> <listitem forEach="${allEvents}" value="${each}"> <listcell label="${each.title}"/> <zscript>String datestr = new SimpleDateFormat("yyyy/MM/dd").format(each.date);</zscript> <listcell label="${datestr}"/> </listitem> </listbox> </zk>
with
events.EventResolver.java
package events; import org.zkoss.xel.VariableResolver; public class EventResolver implements VariableResolver { public Object resolveVariable(String name) { if (name.equals("events")) { return new EventDao().findAll(); } else { return null; } } }
event.zul
<?page title="g2" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> <?variable-resolver class="org.app.EventResolver"?> <?page title="event"?> <zk> <listbox model="@{events}"> <listhead> <listheader label="Title" width="200px"/> <listheader label="Date" width="100px"/> </listhead> <listitem self="@{each='event'}"> <listcell label="@{event.title}"/> <listcell label="@{event.date, converter='org.zkoss.zkplus.databind.DateFormatConverter'}" self="@{format(yyyy/MM/dd)}"/> </listitem> </listbox> </zk>
BTW: I think ZK is a amazing framework and I'm really enjoying learning about it.
Regards,
Eric
Asked: 2011-03-23 13:47:12 +0800
Seen: 575 times
Last updated: Mar 30 '11