Re: Milliseconds out of a java.util.Date that thinks it is a freemarker.template.SimpleDate
Tobias Prinz <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
>
>> [...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