Ugh... this is a really complete example on how to hack a system
if you have control of the velocity templates...

To get it to work, you might insert some text outputs for debuging.
I a project with the young velocity 1.0 I did something similar
using a class with a parameterless constructor, which worked. But
then my workflows needed a bit more, so I added a ClassTool for
instance creation. (I used it to instantiate Hashmaps, XmlTool,
and even a background Thread - to which I could communicate
from other HTML pages via the session! - I can provide the code
if you desire.)

OTOH, I would recommend to look into the ToolboxManager (see the
Velocity-Jira Popular-Issues for the one to use it standalone) for
providing the classes you need via configuration.

Just my 2c!

:) Christoph


Ravikanth L wrote:
Hi Will,
I was trying the following example (to create a custom class instance dynamically in the template) given in the slides
  ## Let's do some mischief and instantiate an arbitrary class
#set ($fileclass = $obj.Class.forName("java.io.File"))
fileclass = $fileclass.name
#set ($stringclass = $obj.Class.forName("java.lang.String"))
stringclass = $stringclass.name
#set ($paramlist = [".."])
#set ($paramarray = $paramlist.toArray())
paramarray size = $ArrayTool.size($paramarray)## to bad that length does not 
work here...
#set ($constructors = $fileclass.constructors)
constructor count = $ArrayTool.size($constructors)
 ## Call the constructor java.io.File(String)
#foreach ($c in $constructors)
constructor[$velocityCount] = $c
#if ($c.toString() == "public java.io.File(java.lang.String)")
#set ($file = $c.newInstance($paramarray))
File was instantiated, here it is:
$file
#end
#end

where $obj is any object instance. I am neither getting the output nor its giving any error in the velocity log. I am using default introspector, not changed anything. Does the deault introspector allows to create newinstance() etc from template?

Please parse the source code or commit logs, maybe this was added at some point.

 Am i missing anything else otherwise?
 Regards,
Ravi
On 12/7/04, Will Glass-Husain <[EMAIL PROTECTED]> wrote:
Thanks for the pointer to my slides from ApacheCon ("Hacking Velocity").

For the record there are slides and sample code available on these topics:

--> VelocityViewServlet
--> Custom directives
--> Custom resource loaders
--> Event handlers
--> Custom introspector
--> Modifying Velocity syntax with JavaCC

I've put the material on the Wiki for more general availability. Hope this
is useful to other users.
http://wiki.apache.org/jakarta-velocity/HackingVelocity

Best, WILL

----- Original Message -----
From: "Shinobu Kawai" <[EMAIL PROTECTED]>
To: "Velocity Users List" <velocity-user@jakarta.apache.org>
Sent: Monday, December 06, 2004 2:12 PM
Subject: Re: [FAQ] How do you check for null?



Hi Henning,


With the slides from Will's talk @ ApacheCon, you should be able to
add a new statement to the VTL so you can do

#ifnull($car.fuel)

Hey, thanks for the idea! Approach #6 : use a custom directive. :)
I'll look into that.

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <[EMAIL PROTECTED]>






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to