Re: Calling a method does not work for me

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, June 17, 2010, 12:30:45 AM, Cody Burleson wrote:

> As it turns out, my errors had to do with returning nulls.

I could have helped, if you had copy-paste the error message. In your
case, when the return value is "missing", the error message says that
the expression anObject.aMethod(aParam) is undefined, while if the
method itself is missing it says that the expression anObject.aMethod
is undefined.

> After some further reading, I learned that there is really no such
> thing as the concept of a null in Freemarker. For example, you can't
> pass a null to a method like you can in Java (though there is a way
> around this). I check for nulls first with <#if object.method()??>
> and that seemed to get me over the hump.

While that FM threat null values just like missing/undefined values is
admittedly a language design mistake (that's why it's fixed in FM 3.0,
but who knows when will that be a stable release), in this case FM is
right, apart from the unhelpful error message. Since the method
doesn't return a boolean value, furthermore it returned null, it can't
be reliably told if the condition was satisfied or not. (Sure, many
languages chose to treat null as logical false, but personally I never
liked that, as null often used to represent the lack of a value. So
then how dare the program assume that it's false, when in fact it has
no information about the thing? Beyond me...)

> Thank you for taking the time to answer, though.
>
> - Cody
>
>
>
> On Wed, Jun 16, 2010 at 4:48 PM, Daniel Dekany <[email protected]> wrote:
>
> Wednesday, June 16, 2010, 8:21:30 PM, Cody Burleson wrote:
>
>> Friends,
>>
>> In a freemarker template, I am trying to call a simple getter method on an object like this:
>>
>> ${indy.getLabel("en")}
>>
>> But freemarker tells me no such method exists. Yet, in my
>> controller, just before giving this object to the view, I call the
>> same public method successfully and print the results. I am new to
>> freemarker, but documentation says you can do this. Any ideas what I
>> might be doing wrong or what I am misunderstanding?
>
> What is visible for templates (and how) depends on the ObjectWrapper,
> which is (normally) part of the freemarker.template.Configuration
> object. In most configurations what you try to do should work, because
> the default ObjectWrapper (and BeansWrapper) does expose methods by
> default. However, even these object wrappers can be configured to hide
> methods, for example with
> theObjectWrapper.setExposureLevel(BeansWrapper.EXPOSE_SAFE).
>
> Another rare but rather mean cause of methods becoming invisible is
> that the object whose method you want to call implements
> java.util.Map. In that case you can only get the Map keys, not the
> methods and JavaBean properties.
>
>> - Cody
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.