Method params take only String/Date/Number. Am I missing something?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Method params take only String/Date/Number. Am I missing something?

Jeremy Chone
Gurus,

I might be missing something obvious. I am trying to pass an Object from one method to another.

I have define 2 TemplateMethodModel  MethodA and MethodB, and I am trying to pass the result (my own Object type) from MethodA into MethodB. However, I cannot seem to pass anything to MethodB outside of String/Date/Number?

--- Code

[#assign resultElem = methodA("from Customer")]
[#assign resultString = methodB(resultElem)]
${resultString}

--- Issue:

1) Either, the resultElem in methodB is of type String (assuming I did not "beanwrap" it in MethodA)
2) Or, I get the following exception: Expecting a string, date or number here, Expression resultElem is instead a freemarker.ext.beans.BeanModel

Is there a way to do this kind of things in Freemarker? I would like to use Freemarker to do "very lightweight" result/xml manipulation in a template language.


Any help greatly appreciated.

Jeremy,



--
Jeremy Chone
blog: http://www.bitsandbuzz.com

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Method params take only String/Date/Number. Am I missing something?

Daniel Dekany
Saturday, August 19, 2006, 6:13:54 PM, Jeremy Chone wrote:

> Gurus,
>
> I might be missing something obvious. I am trying to pass an Object from one method to another.
>
> I have define 2 TemplateMethodModel  MethodA and MethodB, and I am
> trying to pass the result (my own Object type) from MethodA into
> MethodB. However, I cannot seem to pass anything to MethodB outside of String/Date/Number?
>
> --- Code
>
> [#assign resultElem = methodA("from Customer")]
> [#assign resultString = methodB(resultElem)]
> ${resultString}
>
> --- Issue:
>
> 1) Either, the resultElem in methodB is of type String (assuming I
> did not "beanwrap" it in MethodA)
> 2) Or, I get the following exception: Expecting a string, date or
> number here, Expression resultElem is instead a freemarker.ext.beans.BeanModel

It appears to me that that there are two independent issues here.

The first issue is that you want to pass the result of methodA to
methodB. For that to work, the objet wrapper must "unwrap" the value
to the original value, and the BeanWrapper (and its out-of-the-box
subclasses) does exactly that. So you can pass around objects of
whatever wild classes. This works for you as far as I understand.

The second issue is that ${exp} can print only values that are string,
number or date/time in the type-system of FTL (and maybe also boolean,
depending on the settings). For other types FreeMarker can't know how
to display the value as text for a human being (which is the assumed
primary audience). Especially not for a type that is a custom class.
Not without a decent AI, that is... :) So, what is the type of the
return value of methodB? If it's not one of
string/number/date/time/boolean, then you will have to write a macro
or function that converts it. Like <@printProduct myProductValue /> or
${productToString(myProductValue)}.

> Is there a way to do this kind of things in Freemarker?
> I would like to use Freemarker to do "very lightweight" result/xml
> manipulation in a template language.
>
> Any help greatly appreciated.

--
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Method params take only String/Date/Number. Am I missing something?

Jeremy Chone
Thanks a lot for the quick reply.

Yes, I understand the ${} String. MethodB return a string, so I am all set. And actually, I do not even get there, since I get.

The exception arise when I call [#assign resultString = methodB(resultElem)]. MethodA return a custom type ResultElement (which is my own class for now, inherit Object).

If I do not specify CustomObjectWrapper that will wrap the TestObject to BeanModel, then MethodB always get a String.

When I specify my CustomObjectWrapper to BeanWrap TestObject (see code below), then I get the following exception: "Expecting a string, date or number here, Expression resultElem is instead a freemarker.ext.beans.BeanModel"


=== CustomObjectWrapper
public class CustomObjectWrapper extends DefaultObjectWrapper {
   
    public TemplateModel wrap(java.lang.Object obj) throws TemplateModelException{
...
            if (obj instanceof ResultElement){
                BeanModel bm = new BeanModel(obj,(BeansWrapper) ObjectWrapper.BEANS_WRAPPER);
                tm = bm;
            }else{
                tm = super.wrap(obj);
            }
        }
....



Jeremy,


On 8/19/06, Daniel Dekany <[hidden email]> wrote:
Saturday, August 19, 2006, 6:13:54 PM, Jeremy Chone wrote:

> Gurus,
>
> I might be missing something obvious. I am trying to pass an Object from one method to another.
>
> I have define 2 TemplateMethodModel MethodA and MethodB, and I am
> trying to pass the result (my own Object type) from MethodA into
> MethodB. However, I cannot seem to pass anything to MethodB outside of String/Date/Number?
>
> --- Code
>
> [#assign resultElem = methodA("from Customer")]
> [#assign resultString = methodB(resultElem)]
> ${resultString}
>
> --- Issue:
>
> 1) Either, the resultElem in methodB is of type String (assuming I
> did not "beanwrap" it in MethodA)
> 2) Or, I get the following exception: Expecting a string, date or
> number here, Expression resultElem is instead a freemarker.ext.beans.BeanModel

It appears to me that that there are two independent issues here.

The first issue is that you want to pass the result of methodA to
methodB. For that to work, the objet wrapper must "unwrap" the value
to the original value, and the BeanWrapper (and its out-of-the-box
subclasses) does exactly that. So you can pass around objects of
whatever wild classes. This works for you as far as I understand.

The second issue is that ${exp} can print only values that are string,
number or date/time in the type-system of FTL (and maybe also boolean,
depending on the settings). For other types FreeMarker can't know how
to display the value as text for a human being (which is the assumed
primary audience). Especially not for a type that is a custom class.
Not without a decent AI, that is... :) So, what is the type of the
return value of methodB? If it's not one of
string/number/date/time/boolean, then you will have to write a macro
or function that converts it. Like <@printProduct myProductValue /> or
${productToString(myProductValue)}.

> Is there a way to do this kind of things in Freemarker?
> I would like to use Freemarker to do "very lightweight" result/xml
> manipulation in a template language.
>
> Any help greatly appreciated.

--
Best regards,
Daniel Dekany


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user



--
Jeremy Chone
blog: http://www.bitsandbuzz.com

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Method params take only String/Date/Number. Am I missing something?

Daniel Dekany
Saturday, August 19, 2006, 7:27:30 PM, Jeremy Chone wrote:

> Thanks a lot for the quick reply.
>
> Yes, I understand the ${} String. MethodB return a string, so I am
> all set.

Then the interpolation is should work... Chances are low that there is
an FM bug around that.

> And actually, I do not even get there,
> since I get.
>
> The exception arise when I call [#assign resultString =
> methodB(resultElem)]. MethodA return a custom type ResultElement
> (which is my own class for now, inherit Object).
>
> If I do not specify CustomObjectWrapper that will wrap the
> TestObject to BeanModel, then MethodB always get a String.

What is MethodB? Is it a TemplateMethodModel implementation written by
hand? TemplateMethodModel-s is a stupid thing (Shouldn't we deprecate
that, guys?). It can only get String-s as parameter values, hence the
arguments will be casted to that and whatnot. Use
TemplateMethodModelEx instead, it doesn't do any black magic. But, it
must be understood that the ObjectWrapper wraps/unwraps only when
something crosses the boundary of the presentation world (that you can
imagine as an isle inside your application). You see, the wrapped data
is how you want to see the data for presentation purposes, no mater if
the presentation related code is written in FTL or in Java Language.
And the TemplateMethodModelEx implementations that *you* write are
inside the presentation world (since you were writing a method
specifically to be called from FreeMarker templates), so their
parameters will *not* be unwrapped. If you want the parameters be
unwrapped, then theoretically you should call a method that is outside
the presentation world, i.e. you should call a plain Java method from
the template. But, of course you can chose the dirty way and call
((BeansWrapper) theCurrentObjectWrapper).unwrap in the
TemplateMethodModelEx implementation... as a result of the dirtiness
it will not work if somebody will use a wrapper that doesn't extend
BeansWrapper. Admittedly, that's not too probable.

> When I specify my CustomObjectWrapper to BeanWrap TestObject (see
> code below), then I get the following exception: "Expecting a
> string, date or number here, Expression resultElem is instead a  freemarker.ext.beans.BeanModel"
>
>
> === CustomObjectWrapper
> public class CustomObjectWrapper extends DefaultObjectWrapper {
>    
>     public TemplateModel wrap(java.lang.Object obj) throws TemplateModelException{
> ...
>             if (obj instanceof ResultElement){
>                 BeanModel bm = new BeanModel(obj,(BeansWrapper) ObjectWrapper.BEANS_WRAPPER);
>                 tm = bm;
>             }else{
>                 tm =  super.wrap(obj);
>             }
>         }
> ....

I don't see here what causes that... Well, fix the first problem
first, and then we will see what happens with the second one.
>
> Jeremy,

--
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Method params take only String/Date/Number. Am I missing something?

Jeremy Chone
Haaaa.... Ok, "TemplateMethodModelEx" should work.... I did not want the black magic ;). I will try later today, and let you know. Thanks a lot.

Jeremy,


On 8/19/06, Daniel Dekany <[hidden email]> wrote:
Saturday, August 19, 2006, 7:27:30 PM, Jeremy Chone wrote:

> Thanks a lot for the quick reply.
>
> Yes, I understand the ${} String. MethodB return a string, so I am
> all set.

Then the interpolation is should work... Chances are low that there is
an FM bug around that.

> And actually, I do not even get there,
> since I get.
>
> The exception arise when I call [#assign resultString =
> methodB(resultElem)]. MethodA return a custom type ResultElement
> (which is my own class for now, inherit Object).
>
> If I do not specify CustomObjectWrapper that will wrap the
> TestObject to BeanModel, then MethodB always get a String.

What is MethodB? Is it a TemplateMethodModel implementation written by
hand? TemplateMethodModel-s is a stupid thing (Shouldn't we deprecate
that, guys?). It can only get String-s as parameter values, hence the
arguments will be casted to that and whatnot. Use
TemplateMethodModelEx instead, it doesn't do any black magic. But, it
must be understood that the ObjectWrapper wraps/unwraps only when
something crosses the boundary of the presentation world (that you can
imagine as an isle inside your application). You see, the wrapped data
is how you want to see the data for presentation purposes, no mater if
the presentation related code is written in FTL or in Java Language.
And the TemplateMethodModelEx implementations that *you* write are
inside the presentation world (since you were writing a method
specifically to be called from FreeMarker templates), so their
parameters will *not* be unwrapped. If you want the parameters be
unwrapped, then theoretically you should call a method that is outside
the presentation world, i.e. you should call a plain Java method from
the template. But, of course you can chose the dirty way and call
((BeansWrapper) theCurrentObjectWrapper).unwrap in the
TemplateMethodModelEx implementation... as a result of the dirtiness
it will not work if somebody will use a wrapper that doesn't extend
BeansWrapper. Admittedly, that's not too probable.

> When I specify my CustomObjectWrapper to BeanWrap TestObject (see
> code below), then I get the following exception: "Expecting a
> string, date or number here, Expression resultElem is instead a  freemarker.ext.beans.BeanModel"
>
>
> === CustomObjectWrapper
> public class CustomObjectWrapper extends DefaultObjectWrapper {
>
> public TemplateModel wrap(java.lang.Object obj) throws TemplateModelException{
> ...
> if (obj instanceof ResultElement){
> BeanModel bm = new BeanModel(obj,(BeansWrapper) ObjectWrapper.BEANS_WRAPPER );
> tm = bm;
> }else{
> tm =  super.wrap(obj);
> }
> }
> ....

I don't see here what causes that... Well, fix the first problem
first, and then we will see what happens with the second one.
>
> Jeremy,

--
Best regards,
Daniel Dekany


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user



--
Jeremy Chone
blog: http://www.bitsandbuzz.com

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Method params take only String/Date/Number. Am I missing something?

Jeremy Chone
Thanks Daniel,

it works now (with TemplateMethodEx). Sorry, this was my fault. I spent couple of hours on this one, and did not fully read the JavaDoc.

Tx for your responsiveness and patience. Freemarker rocks! The documentation is also very good.

Jeremy,


On 8/19/06, Jeremy Chone <[hidden email]> wrote:
Haaaa.... Ok, "TemplateMethodModelEx" should work.... I did not want the black magic ;). I will try later today, and let you know. Thanks a lot.

Jeremy,


On 8/19/06, Daniel Dekany <[hidden email]> wrote:
Saturday, August 19, 2006, 7:27:30 PM, Jeremy Chone wrote:

> Thanks a lot for the quick reply.
>
> Yes, I understand the ${} String. MethodB return a string, so I am
> all set.

Then the interpolation is should work... Chances are low that there is
an FM bug around that.

> And actually, I do not even get there,
> since I get.
>
> The exception arise when I call [#assign resultString =
> methodB(resultElem)]. MethodA return a custom type ResultElement
> (which is my own class for now, inherit Object).
>
> If I do not specify CustomObjectWrapper that will wrap the
> TestObject to BeanModel, then MethodB always get a String.

What is MethodB? Is it a TemplateMethodModel implementation written by
hand? TemplateMethodModel-s is a stupid thing (Shouldn't we deprecate
that, guys?). It can only get String-s as parameter values, hence the
arguments will be casted to that and whatnot. Use
TemplateMethodModelEx instead, it doesn't do any black magic. But, it
must be understood that the ObjectWrapper wraps/unwraps only when
something crosses the boundary of the presentation world (that you can
imagine as an isle inside your application). You see, the wrapped data
is how you want to see the data for presentation purposes, no mater if
the presentation related code is written in FTL or in Java Language.
And the TemplateMethodModelEx implementations that *you* write are
inside the presentation world (since you were writing a method
specifically to be called from FreeMarker templates), so their
parameters will *not* be unwrapped. If you want the parameters be
unwrapped, then theoretically you should call a method that is outside
the presentation world, i.e. you should call a plain Java method from
the template. But, of course you can chose the dirty way and call
((BeansWrapper) theCurrentObjectWrapper).unwrap in the
TemplateMethodModelEx implementation... as a result of the dirtiness
it will not work if somebody will use a wrapper that doesn't extend
BeansWrapper. Admittedly, that's not too probable.

> When I specify my CustomObjectWrapper to BeanWrap TestObject (see
> code below), then I get the following exception: "Expecting a
> string, date or number here, Expression resultElem is instead a  freemarker.ext.beans.BeanModel"
>
>
> === CustomObjectWrapper
> public class CustomObjectWrapper extends DefaultObjectWrapper {
>
> public TemplateModel wrap(java.lang.Object obj) throws TemplateModelException{
> ...
> if (obj instanceof ResultElement){
> BeanModel bm = new BeanModel(obj,(BeansWrapper) ObjectWrapper.BEANS_WRAPPER );
> tm = bm;
> }else{
> tm =  super.wrap(obj);
> }
> }
> ....

I don't see here what causes that... Well, fix the first problem
first, and then we will see what happens with the second one.
>
> Jeremy,

--
Best regards,
Daniel Dekany


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&amp;kid=120709&amp;bid=263057&amp;dat=121642" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
<a href="https://lists.sourceforge.net/lists/listinfo/freemarker-user" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">https://lists.sourceforge.net/lists/listinfo/freemarker-user



--
Jeremy Chone
blog: <a href="http://www.bitsandbuzz.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.bitsandbuzz.com



--
Jeremy Chone
blog: http://www.bitsandbuzz.com

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user