You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by pf...@iol.pt on 2004/10/09 17:59:06 UTC

Can I perform math operations on longs with the #set() directive?

Hi all, 

 I'm using velocity to generate mails based on several templates. 

 I hava an application that stores certain information in *longs* (such as duration in seconds and storage space in bytes).

 I'd like to send emails with this information, but I'd like to convert it to different units for readability purposes (bytes to Mb or Gb, and seconds to days). It nakes sense to do this in the template (presentation layer), because if someone wants to have a diferent kind of unit they can change the template (which is stored in the DB) and I don't have to rebuild and redeploy the whole project.

 I'd like to do something like #set($storage_mb = $storage_bytes/(1024*1024)) but, unfortunantlly the #set() directive only allows operations in integers.

 Can someone sujest another way to do this in the template?

--
Thanx
Pedro Pires

________________________________________________________________________________
A protecção do e-mail contra vírus é cada vez mais necessária!
Proteja a sua Caixa de Correio: http://www.iol.pt/correio/rodape.php?dst=0409301


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


Re: Can I perform math operations on longs with the #set() directive?

Posted by Kishore Senji <ks...@gmail.com>.
Use Math Tool.

http://jakarta.apache.org/velocity/tools/generic/MathTool.html




On Sat, 09 Oct 2004 16:59:06 +0100, pfpires.velocity@iol.pt
<pf...@iol.pt> wrote:
> Hi all,
> 
> I'm using velocity to generate mails based on several templates.
> 
> I hava an application that stores certain information in *longs* (such as duration in seconds and storage space in bytes).
> 
> I'd like to send emails with this information, but I'd like to convert it to different units for readability purposes (bytes to Mb or Gb, and seconds to days). It nakes sense to do this in the template (presentation layer), because if someone wants to have a diferent kind of unit they can change the template (which is stored in the DB) and I don't have to rebuild and redeploy the whole project.
> 
> I'd like to do something like #set($storage_mb = $storage_bytes/(1024*1024)) but, unfortunantlly the #set() directive only allows operations in integers.
> 
> Can someone sujest another way to do this in the template?
> 
> --
> Thanx
> Pedro Pires
> 
> ________________________________________________________________________________
> A protecção do e-mail contra vírus é cada vez mais necessária!
> Proteja a sua Caixa de Correio: http://www.iol.pt/correio/rodape.php?dst=0409301
> 
> ---------------------------------------------------------------------
> 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


Re: [TOOLS] LayoutServlet finds layouts, but not templates in CP

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi,

> And here's the patch to do so.
Where did it go?  I'll try again.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

Re: [TOOLS] LayoutServlet finds layouts, but not templates in CP

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi,

> The root of the problem is that
> java.util.jar.JarFile#getJarEntry(String name) will not return
> anything if a "/" is leading the name.  This causes
> org.apache.catalina.loader.WebappClassLoader#findResourceInternal(String
> name, String path) to return null.  This causes
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader#getResourceStream(String
> name) to return null.  This causes your ResourceNotFoundException.
> 
> If we fix ClasspathResourceLoader so it trims the leading "/"s like in
> WebappLoader, it will work.  I'm not sure if this is the right fix,
> though.  It might just be a Tomcat issue.  I'll attach the patch
> anyways.
You know what?  I think it's not the ClasspathResourceLoader's
responsibility.  It's just doing its stuff.  A better place to trim the
leading "/"s is where the template path is being created, namely
o.a.v.t.view.servlet.VelocityViewServlet#handleRequest(...).

And here's the patch to do so.

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


RE: [TOOLS] LayoutServlet finds layouts, but not templates in CP

Posted by Tim Colson <tc...@cisco.com>.
> Are you specifying CommonsLogLogSystem as the LogSystem class? 
> Otherwise, I don't think you need it.  At least I didn't need it.  
I didn't spec anything, just grabbed the examples\layout.war and dropped it
into tomcat 4.0.6 and it complained about commons logging. YMMV

> java.util.jar.JarFile#getJarEntry(String name) will not return
> anything if a "/" is leading the name. 
> .... This causes your ResourceNotFoundException.
> It might just be a Tomcat issue. I'll attach the patch
> anyways.

Thanks Shinobu -- that worked great. :-)

Cheers,
Timo



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


Re: [TOOLS] LayoutServlet finds layouts, but not templates in CP

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Timo,

>  Received a question from a colleague trying to load templates from the
> classpath (i.e. inside a jar in WEB-INF/lib) and getting: Unable to find
> resource errors.
> 
> I've been using the classpath resource loader in another project without
> problems, but not for the main template -- just the layout.
> 
> I modified the velocity-tools layout example app to test.
> I see the same problem. :-(
> 
> I made a jar with these two files and added to the Velocity Tools layout
> example:
> cptimo.vm          -> "Howdy from inside the template.jar"
> layout/cplayout.vm -> "classpath layout $screencontent"
> 
> http://localhost/layout/index.vm?layout=cplayout.vm -> works great
> http://localhost/layout/cptimo.vm -> fails:

## snip

> I had to add commons-logging.jar to get the servlet to init (Marino/Nathan
> -- is there a reason that isn't included?)
Are you specifying CommonsLogLogSystem as the LogSystem class? 
Otherwise, I don't think you need it.  At least I didn't need it.  :)

## snip

> Any ideas?
The root of the problem is that
java.util.jar.JarFile#getJarEntry(String name) will not return
anything if a "/" is leading the name.  This causes
org.apache.catalina.loader.WebappClassLoader#findResourceInternal(String
name, String path) to return null.  This causes
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader#getResourceStream(String
name) to return null.  This causes your ResourceNotFoundException.

If we fix ClasspathResourceLoader so it trims the leading "/"s like in
WebappLoader, it will work.  I'm not sure if this is the right fix,
though.  It might just be a Tomcat issue.  I'll attach the patch
anyways.

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

[TOOLS] LayoutServlet finds layouts, but not templates in CP

Posted by Tim Colson <tc...@cisco.com>.
Howdy folks -
  Received a question from a colleague trying to load templates from the
classpath (i.e. inside a jar in WEB-INF/lib) and getting: Unable to find
resource errors.

I've been using the classpath resource loader in another project without
problems, but not for the main template -- just the layout. 

I modified the velocity-tools layout example app to test.
I see the same problem. :-(

I made a jar with these two files and added to the Velocity Tools layout
example:
cptimo.vm          -> "Howdy from inside the template.jar"
layout/cplayout.vm -> "classpath layout $screencontent"

http://localhost/layout/index.vm?layout=cplayout.vm -> works great
http://localhost/layout/cptimo.vm -> fails:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find
resource '/cptimo.vm'
	at
org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(Resour
ceManagerImpl.java:458)
	at
org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(Resourc
eManagerImpl.java:341)
	at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java
:831)
	at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java
:813)
	at
org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.ja
va:285)
	at
org.apache.velocity.tools.view.servlet.VelocityViewServlet.getTemplate(Unkno
wn Source)
------

I'm running inside Tomcat 4.0.6, using the HEAD of velocity
tools-examples-layout app.

I had to add commons-logging.jar to get the servlet to init (Marino/Nathan
-- is there a reason that isn't included?)

I added the template.jar from above to WEB-INF/lib and this to the
velocity.properties

#---------------------------------------------------------------------------
-
# RESOURCE loaders
#---------------------------------------------------------------------------
-
resource.loader = class, webapp
#
# ClasspathResourceLoader
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class =
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
# WebappResourceLoader
webapp.resource.loader.description = Velocity Webapp Resource Loader
webapp.resource.loader.class=org.apache.velocity.tools.view.servlet.WebappLo
ader
webapp.resource.loader.path=/WEB-INF/pages/,/
# Should be set to TRUE everywhere but LOCAL workstation
webapp.resource.loader.cache=false
-----------------
Any ideas?

Thanks,
Timo


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