Trouble calling methods in a Struts 2 action

6 messages Options
Embed this post
Permalink
Christopher Maloof

Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It
mostly works great: my templates are retrieving bean information with no
problem.  However, I can't figure out how to call methods defined in my
action classes -- in fact I have no idea why it isn't working as I expected.

If my action class looks like this:

public class MyAction extends MyActionSupport {
    public String getMyValue() {
       return "hello";
    }
    public String myMethod() {
       return "world";
    }
}

then ${myValue} or ${myValue.toUpperCase()} works fine, as does
<@s.property value="myMethod()"/>, but ${myMethod()}results in a
"Expression myMethod is undefined" error.  The docs
(http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_methodcall)
seem to think this should be perfectly fine, as far as I can tell.

Any idea why Freemarker isn't seeing the top-level action methods unless
I use <@s.property>?

Thanks for any help -
Chris


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
Friday, November 6, 2009, 6:32:05 PM, Christopher Maloof wrote:

> I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It
> mostly works great: my templates are retrieving bean information with no
> problem.  However, I can't figure out how to call methods defined in my
> action classes -- in fact I have no idea why it isn't working as I expected.
>
> If my action class looks like this:
>
> public class MyAction extends MyActionSupport {
>     public String getMyValue() {
>        return "hello";
>     }
>     public String myMethod() {
>        return "world";
>     }
> }
>
> then ${myValue} or ${myValue.toUpperCase()} works fine, as does
> <@s.property value="myMethod()"/>, but ${myMethod()}results in a
> "Expression myMethod is undefined" error.  The docs
> (http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_methodcall)
> seem to think this should be perfectly fine, as far as I can tell.
>
> Any idea why Freemarker isn't seeing the top-level action methods unless
> I use <@s.property>?

What's the data-model in Struts? It had to be the MyAction instance,
or else the methods will no be visible.

> Thanks for any help -
> Chris
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Christopher Maloof

Re: Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
On 11/6/2009 2:44 PM, Daniel Dekany wrote:
Friday, November 6, 2009, 6:32:05 PM, Christopher Maloof wrote:

  
I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It 
mostly works great: my templates are retrieving bean information with no
problem.  However, I can't figure out how to call methods defined in my
action classes -- in fact I have no idea why it isn't working as I expected.

If my action class looks like this:

public class MyAction extends MyActionSupport {
    public String getMyValue() {
       return "hello";
    }
    public String myMethod() {
       return "world";
    }
}

then ${myValue} or ${myValue.toUpperCase()} works fine, as does 
<@s.property value="myMethod()"/>, but ${myMethod()}results in a 
"Expression myMethod is undefined" error.  The docs 
(http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_methodcall)
seem to think this should be perfectly fine, as far as I can tell.

Any idea why Freemarker isn't seeing the top-level action methods unless
I use <@s.property>?
    

What's the data-model in Struts? It had to be the MyAction instance,
or else the methods will no be visible.
The template is mapped to the correct action, if that's what you're asking.  These things work:
${myValue}  (indirect access to bean getter)
${myValue.toUpperCase()}  (method call on object)
<@s.property value="myMethod()"/>   (Struts tag calling a method)

But these things don't:
${myMethod()}
${getMyValue()}  (plain method calls as described in the docs)

Chris


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
Thing is, FreeMarker is very flexible when it comes to building the
data-model (mostly done with so called object wrapping), and I don't
know what the Struts guys done. If they use the action object itself
to be the data-model, with some typical object wrapper setup the
public methods are visible. But then it seems they decided that they
will not be visible... they can do that, it's up to them. So maybe
it's better to ask them.


Friday, November 6, 2009, 9:22:36 PM, Christopher Maloof wrote:

> On 11/6/2009 2:44 PM, Daniel Dekany wrote:
> Friday, November 6, 2009, 6:32:05 PM, Christopher Maloof wrote:
>
>  
> I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It
> mostly works great: my templates are retrieving bean information with no
> problem.  However, I can't figure out how to call methods defined in my
> action classes -- in fact I have no idea why it isn't working as I expected.
>
> If my action class looks like this:
>
> public class MyAction extends MyActionSupport {
>     public String getMyValue() {
>        return "hello";
>     }
>     public String myMethod() {
>        return "world";
>     }
> }
[snip]

> The template is mapped to the correct action, if that's what you're asking.  These things work:
> ${myValue}  (indirect access to bean getter)
> ${myValue.toUpperCase()}  (method call on object)
> <@s.property value="myMethod()"/>   (Struts tag calling a method)
>
> But these things don't:
> ${myMethod()}
> ${getMyValue()}  (plain method calls as described in the docs)
>
> Chris

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Christopher Maloof

Re: Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
You were right, the Struts list was the place to ask.  The trick is to use the built-in "action" variable to refer to the action directly, like ${action.myMethod()}.  I'm not sure why the public methods aren't made available otherwise, but it's an easy enough solution.

http://markmail.org/message/upuwtwdd45cqwrwb

Thanks,
Chris

On 11/7/2009 1:20 AM, Daniel Dekany wrote:
Thing is, FreeMarker is very flexible when it comes to building the
data-model (mostly done with so called object wrapping), and I don't
know what the Struts guys done. If they use the action object itself
to be the data-model, with some typical object wrapper setup the
public methods are visible. But then it seems they decided that they
will not be visible... they can do that, it's up to them. So maybe
it's better to ask them.


Friday, November 6, 2009, 9:22:36 PM, Christopher Maloof wrote:

  
On 11/6/2009 2:44 PM, Daniel Dekany wrote: 
Friday, November 6, 2009, 6:32:05 PM, Christopher Maloof wrote:

  
I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It 
mostly works great: my templates are retrieving bean information with no
problem.  However, I can't figure out how to call methods defined in my
action classes -- in fact I have no idea why it isn't working as I expected.

If my action class looks like this:

public class MyAction extends MyActionSupport {
    public String getMyValue() {
       return "hello";
    }
    public String myMethod() {
       return "world";
    }
}
    
[snip]
  
The template is mapped to the correct action, if that's what you're asking.  These things work:
${myValue}  (indirect access to bean getter)
${myValue.toUpperCase()}  (method call on object)
<@s.property value="myMethod()"/>   (Struts tag calling a method)

But these things don't:
${myMethod()}
${getMyValue()}  (plain method calls as described in the docs)

Chris
    

  


-- 
Christopher Maloof, M.S.
Software Developer, Medical Oncology, Informatics Shared Resource
Kimmel Cancer Center, Thomas Jefferson University
Phone: 215-503-9275  Fax: 215-955-9093 

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Payne, Matthew

Re: Trouble calling methods in a Struts 2 action

Reply Threaded More More options
Print post
Permalink
In reply to this post by Daniel Dekany
I am using the same stack as use albeit I am using my own freemarker manager (not struts built-in).  

I don't have any problems calling "getters" from the action in ftl.
e.g. ${myValue}


-----Original Message-----
From: Daniel Dekany [mailto:[hidden email]]
Sent: Friday, November 06, 2009 2:44 PM
To: Christopher Maloof
Cc: [hidden email];
[hidden email]
Subject: Re: [FreeMarker-user] Trouble calling methods in a Struts 2
action


Friday, November 6, 2009, 6:32:05 PM, Christopher Maloof wrote:

> I'm just starting to use FreeMarker (2.3.15) with Struts 2.1.8.  It
> mostly works great: my templates are retrieving bean information with no
> problem.  However, I can't figure out how to call methods defined in my
> action classes -- in fact I have no idea why it isn't working as I expected.
>
> If my action class looks like this:
>
> public class MyAction extends MyActionSupport {
>     public String getMyValue() {
>        return "hello";
>     }
>     public String myMethod() {
>        return "world";
>     }
> }
>
> then ${myValue} or ${myValue.toUpperCase()} works fine, as does
> <@s.property value="myMethod()"/>, but ${myMethod()}results in a
> "Expression myMethod is undefined" error.  The docs
> (http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_methodcall)
> seem to think this should be perfectly fine, as far as I can tell.
>
> Any idea why Freemarker isn't seeing the top-level action methods unless
> I use <@s.property>?

What's the data-model in Struts? It had to be the MyAction instance,
or else the methods will no be visible.

> Thanks for any help -
> Chris
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
This message, including any attachments, is intended only for the recipient(s)
named above. It may contain confidential and privileged information. If you have
received this communication in error, please notify the sender immediately and
destroy or delete the original message. Also, please be aware that if you are not
the intended recipient, any review, disclosure, copying, distribution or any
action or reliance based on this message is prohibited by law.  


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user