Milliseconds out of a java.util.Date that thinks it is a freemarker.template.SimpleDate

3 messages Options
Embed this post
Permalink
Tierlieb

Milliseconds out of a java.util.Date that thinks it is a freemarker.template.SimpleDate

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)

Hi there,

I got a set of pretty simple macros for printing out dates. One for giving only the day, one for the time, one for timezone. These all work fine, using the built-in features like ?string("dddd-mm-yy").


Now I try (for debugging purposes) to write one that gives me the milliseconds since 1970 (which is not covered by built-in functions).

For a java.util.Date, this is simple, right?

wrapper.getMyDate.getTime()

So I tried a naive approach in Freemarker:

wrapper.myDate.time

Sadly, this makes the system cry out:
Expected hash. wrapper.myDate evaluated instead to freemarker.template.SimpleDate


I wonder why that is happening.
My assumption, though I did not find anything in the docs about it: There are obviously classes that get treated differently by FreeMarker. Some are just wrapped, but others get special features. java.util.Date is wrapped into freemarker.template.SimpleDate. The latter misses the former's getTime() method. Right?
If so: How can I prevent this from happening?
If not: How else does it work, what do I need to do?

 

Thanks in advance,

Tobias


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Milliseconds out of a java.util.Date that thinks it is a freemarker.template.SimpleDate

Reply Threaded More More options
Print post
Permalink
Monday, October 5, 2009, 11:29:46 AM, Prinz, Tobias wrote:

> Hi there,
>
> I got a set of pretty simple macros for printing out dates. One for
> giving only the day, one for the time, one for timezone. These all
> work fine, using the built-in features like ?string("dddd-mm-yy").
>
> Now I try (for debugging purposes) to write one that gives me the
> milliseconds since 1970 (which is not covered by built-in functions).
>
> For a java.util.Date, this is simple, right?
>
> wrapper.getMyDate.getTime()
>
> So I tried a naive approach in Freemarker:
>
> wrapper.myDate.time
>
> Sadly, this makes the system cry out:
> Expected hash. wrapper.myDate evaluated instead to freemarker.template.SimpleDate
>
>
> I wonder why that is happening.
> My assumption, though I did not find anything in the docs about it:
> There are obviously classes that get treated differently by
> FreeMarker. Some are just wrapped, but others get special features.
> java.util.Date is wrapped into freemarker.template.SimpleDate. The
> latter misses the former's getTime() method. Right?

The thing is, originally FreeMarker didn't exposed methods and
JavaBean properties at all. Now it has the BeansWrapper that does, but
for backward compatibility the DefaultObjectWrapper is used which
still doesn't expose the method/properties for "well-known" classes
like Date.

> If so: How can I prevent this from happening?

Use plain BeansWrapper... where you configure FreeMarker add this:

  BeansWrapper wrapper = new BeansWrapper();
  wrapper.setSimpleMapWrapper(true);
  cfg.setObjectWrapper(wrapper);

Where cfg is the Configuration objet. Now, if you do it on a big
complex system, you better do some testing, because the template will
see the data-model a bit differently now (like you can write
${myDate.time}). It mostly shouldn't break anything, but who knows...

If this you find too risky, you can write a TemplateMethodModelEx that
extract the millisecond part.

Also, I think I will add a built-in for this in 2.3.16... it seems
it's often needed. myDate?time_millis or something. I think 2.3.16
will be released within two weeks or so, but I can add this built-in
much earlier in the nightly builds if you would use that. Would you?

> If not: How else does it work, what do I need to do?
>  
> Thanks in advance,
> Tobias

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Tierlieb

Re: Milliseconds out of a java.util.Date that thinks it is a freemarker.template.SimpleDate

Reply Threaded More More options
Print post
Permalink

>
>> [...Using a Date object...]
>> So I tried a naive approach in Freemarker:
>>
>> wrapper.myDate.time
>>
>> Sadly, this makes the system cry out:
>> Expected hash. wrapper.myDate evaluated instead to
>> freemarker.template.SimpleDate
>>
>>
>> I wonder why that is happening.
>> My assumption, though I did not find anything in the docs about it:
>> There are obviously classes that get treated differently by
>> FreeMarker. Some are just wrapped, but others get special features.
>> java.util.Date is wrapped into freemarker.template.SimpleDate. The
>> latter misses the former's getTime() method. Right?
>
> The thing is, originally FreeMarker didn't exposed methods and
> JavaBean properties at all. Now it has the BeansWrapper that does, but
> for backward compatibility the DefaultObjectWrapper is used which
> still doesn't expose the method/properties for "well-known" classes
> like Date.
>
Ah, okay. I just thought it was me not understand an important part of
the system.
Good, thanks for the hint.

>> If so: How can I prevent this from happening?
>
> Use plain BeansWrapper... where you configure FreeMarker add this:
>
> BeansWrapper wrapper = new BeansWrapper();
> wrapper.setSimpleMapWrapper(true);
> cfg.setObjectWrapper(wrapper);
>
> Where cfg is the Configuration objet. Now, if you do it on a big
> complex system, you better do some testing, because the template will
> see the data-model a bit differently now (like you can write
> ${myDate.time}). It mostly shouldn't break anything, but who knows...
>
> If this you find too risky, you can write a TemplateMethodModelEx that
> extract the millisecond part.
I think I'll use the second way to get around it. Thanks.

> Also, I think I will add a built-in for this in 2.3.16... it seems
> it's often needed. myDate?time_millis or something.
Sounds great to me!

> I think 2.3.16
> will be released within two weeks or so, but I can add this built-in
> much earlier in the nightly builds if you would use that. Would you?
I got around the problem for now, so I can wait until the next release.
No need to hurry.

Thank you very much,
Tobias

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user