You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by tk...@netcourrier.com on 2003/12/05 08:53:27 UTC

velocityTool and conversions

Hi,

Within a velocity template, How can I make conversion between basic types such as byte, int, etc... and their Wrappers such as Byte, Integer, etc... ?

Is there any way to convert String to int, double, byte, etc ...
Any example is welcome.

In Java this is done by : int value = Integer.parseInt(stringValue); or String stringValue = String.valueOf(value);

How such things can be done with velocity, or velocityTool?

More generally, is it possible to use java kit (jdk) operations through velocity variables?

-------------------------------------------------------------
NetCourrier, votre bureau virtuel sur Internet : Mail, Agenda, Clubs, Toolbar...
Web/Wap : www.netcourrier.com
Téléphone/Fax : 08 92 69 00 21 (0,34 € TTC/min)
Minitel: 3615 NETCOURRIER (0,16 € TTC/min)


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


Re: velocityTool and conversions

Posted by "J. B. Rainsberger" <jb...@rogers.com>.
tkchris@netcourrier.com wrote:

> Hi,
> 
> Within a velocity template, How can I make conversion between basic types such as byte, int, etc... and their Wrappers such as Byte, Integer, etc... ?

Why are you doing this in the template? Are you forced to do it by a 
certain design? Typically, once objects reach your template, they ought 
to all be Strings, Maps of Strings to Strings and Collections of Strings.
-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand


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


Re: velocityTool and conversions

Posted by Ch...@dlr.de.
tkchris@netcourrier.com wrote:
> Hi,
> 
> Within a velocity template, How can I make conversion between basic 
> types such as byte, int, etc... and their Wrappers such as Byte, 
> Integer, etc... ?
> 
> Is there any way to convert String to int, double, byte, etc ...
> Any example is welcome.

Velocity uses the java reflection API, where no primitives exists, but
these are treated transparently by their object counterpart.

So as a short answer: you do not need to care if int or Integer,
byte or Byte, etc. The reflecion API takes care of this:
#set( $byte = $foo.somethingThatReturns_byte() )
#set( $bar = $foo.methodThatRequires_byte_Parameter($byte) )

> 
> In Java this is done by : int value = Integer.parseInt(stringValue); 
> or String stringValue = String.valueOf(value);
> 
> How such things can be done with velocity, or velocityTool?

For String<->Number conversions it is a bit more diverse...

The current velocity releases only have built-in Integer support -
   #set( $myInt = 0 ) creates an `new Integer((int) 0)` for you. The
velocity introspection engine allows you to use all methods of the
Integer API - so you can then do:
   #set( $myIntFromString = $myInt.parseInt("12345") ).

The same applies to implicit string support. But String.valueOf(value)
is really not necesary in velocity since it is the same as the
toString() of the Number objects (to my knowlege - correct me if
I'm wrong). So you do not need:
   #set( #string = $myContextString.valueOf($myInt) )
since it is simpler to say:
   #set( #string = "$myInt" )

Somewhere in the works (either in bugzilla or in the CVS trunk)
a contributor has provided patches to extend velocity to undestand
other Number types. Please search the archives to learn more about
the state (last thread on this subject is less than a month old).

> 
> More generally, is it possible to use java kit (jdk) operations 
> through velocity variables?
> 

Currently other primitives and arrays need Tool support. A good
and immediate starting point is the VelocityViewServlet, Layout*
and ToolManager which allow you to place instances of any object
in the context for your use. This would enable things like
   #set( $long = $myContextLong.parseLong("12345678901234567890")


P.S. but beware - in this List there are some defenders of MVC
separation that will howl at you (an me) if you put application
logic into a template. OTOH I've successfully used Velocity as
the control-layer and view-layer in one of my application!

I also use velocity for anakia-like XML and CVS transformations
from the command line (e.g. a controller template reads in a
CVS-file and creates an output file per row using a parsed other
template). I can post the template-code if someone wishes.

Cheers,
Christoph

	


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