You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Fakir Nooruddin <fs...@gmail.com> on 2006/08/08 20:06:43 UTC

Calling methods on an object from a Velocity Template

Hello,

I have been stumped by this for several hours. I have looked at
available documentation & archives of this list, but to no avail.
Hoping that someone on this list can set me straight.

All that I am trying to do is call a public non-static method of an
object that I pass into my template. The output of the template is:

 Test is: TestItem@a7dd39

Test is: $entry.getInfo()

This tells me that the template knows that the object is of type
TestItem, but it still refuses to give me the results of the getInfo()
call. Instead, I get an "undefined reference" warning for the
getInfo() call. Further, if I over-ride the toString method in the
TestInfo class, the template correctly displays the results.

The TestItem class is:

class TestItem {

    private String Info;

    public TestItem()
    {
	Info = "value of Info";
    }

    public String getInfo()
    {
	return Info;
    }

    public void setInfo( String str )
    {
	Info = str;
    }
}

And the template is:

<html>

<h2> Test is: $entry </h2> <br>
<h2> Test is: $entry.getInfo() </h2> <br>

</html>

My Java code populates the context like this:

ctx.put("theList", personList );

TestItem ti = new TestItem();

ctx.put("entry", ti);


Thanks in advance for your help!

-Nooruddin

p.s. I am using Velocity 1.4 with Tomcat 5.5 on Windows XP.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Calling methods on an object from a Velocity Template

Posted by Martin Tilsted <ti...@daimi.au.dk>.
That's a classic error :}

The CLASS also need to be public, not just the methods.

Citat Fakir Nooruddin <fs...@gmail.com>:

> Hello,
>
> I have been stumped by this for several hours. I have looked at
> available documentation & archives of this list, but to no avail.
> Hoping that someone on this list can set me straight.
>
> All that I am trying to do is call a public non-static method of an
> object that I pass into my template. The output of the template is:
>
>  Test is: TestItem@a7dd39
>
> Test is: $entry.getInfo()
>
> This tells me that the template knows that the object is of type
> TestItem, but it still refuses to give me the results of the getInfo()
> call. Instead, I get an "undefined reference" warning for the
> getInfo() call. Further, if I over-ride the toString method in the
> TestInfo class, the template correctly displays the results.
>
> The TestItem class is:
>
> class TestItem {
>
>     private String Info;
>
>     public TestItem()
>     {
> 	Info = "value of Info";
>     }
>
>     public String getInfo()
>     {
> 	return Info;
>     }
>
>     public void setInfo( String str )
>     {
> 	Info = str;
>     }
> }
>
> And the template is:
>
> <html>
>
> <h2> Test is: $entry </h2> <br>
> <h2> Test is: $entry.getInfo() </h2> <br>
>
> </html>
>
> My Java code populates the context like this:
>
> ctx.put("theList", personList );
>
> TestItem ti = new TestItem();
>
> ctx.put("entry", ti);
>
>
> Thanks in advance for your help!
>
> -Nooruddin
>
> p.s. I am using Velocity 1.4 with Tomcat 5.5 on Windows XP.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>




---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org