0

Hibernate example failing, can't find each.date???

asked 2011-03-23 13:47:12 +0800

ejchet gravatar image ejchet
18

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

delete flag offensive retag edit

14 Replies

Sort by ยป oldest newest

answered 2011-03-23 15:04:01 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

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.

link publish delete flag offensive edit

answered 2011-03-23 15:20:45 +0800

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

updated 2011-03-23 15:21:18 +0800

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

link publish delete flag offensive edit

answered 2011-03-23 15:58:43 +0800

ejchet gravatar image ejchet
18

Interesting because this is from your example;

http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/Integration/Hibernate

How would you write it to format the date object?

Thanks,

Eric

link publish delete flag offensive edit

answered 2011-03-23 16:17:24 +0800

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

Please, show us your complete code.

link publish delete flag offensive edit

answered 2011-03-23 16:22:23 +0800

ejchet gravatar image ejchet
18

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

link publish delete flag offensive edit

answered 2011-03-23 17:03:45 +0800

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

do you have the other stuff like the EventDAO (for the demo data) implemented too ?

link publish delete flag offensive edit

answered 2011-03-23 18:43:35 +0800

ejchet gravatar image ejchet
18

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

link publish delete flag offensive edit

answered 2011-03-24 18:23:04 +0800

twiegand gravatar image twiegand
1807 3

Wouldn't it be easier to use databinding and a typeconverter to format the date for each field?

link publish delete flag offensive edit

answered 2011-03-25 07:49:54 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

DateFormatConverter

link publish delete flag offensive edit

answered 2011-03-25 10:43:07 +0800

ejchet gravatar image ejchet
18

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

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-03-23 13:47:12 +0800

Seen: 575 times

Last updated: Mar 30 '11

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