Questions tagged [velocity]

Apache Velocity is a Java-based template engine. It can be used to dynamically generate web pages, email messages, source code, or any other text file.

Apache Velocity is a Java-based template engine. It can be used to dynamically generate web pages, email messages, source code, or any other text file.

Apache Velocity is a project of the Apache Software Foundations (ASF).

More information is available at the project home page: http://velocity.apache.org.

From the developer perspective, the fundamental pattern for rendering a text file is:

  • Create and initialize a VelocityEngine.
  • Add your data objects to a Context (essentially, a map of objects available for use in the template)
  • Choose a template
  • Merge the template with the context.

This can be done in a few lines of Java code, or you can use one of several frameworks to integrate this with your application.

One of the most popular web application frameworks which integrates Velocity is the Spring Framework. For simple servlet based applications, most developers use one of the Velocity Tools servlets (also available from the Apache Velocity project).

Useful Online Resources

  1. Velocity User Guide
  2. Velocity Developer Guide
2370 questions
38
votes
5 answers

Velocity editor plugin for Eclipse?

I've seen Veloedit, which seems to have good syntax highlighting but doesn't allow tab characters in the file being edited (wtf?) and also has no understanding of HTML. With a little bit of googling I've found Veloecipse, which claims to build upon…
matt b
  • 138,234
  • 66
  • 282
  • 345
37
votes
3 answers

Velocity, what's the most efficient way to check if a string is empty and not null

I often have cases when a string value is absent and/or empty. Is this the best way to test this condition? #if( $incentive.disclaimer && $!incentive.disclaimer != '' ) $incentive.disclaimer #end
rsturim
  • 6,756
  • 15
  • 47
  • 59
36
votes
6 answers

Velocity: Is a any way to check if variable is defined

I want to include one template nested into others cont1, cont2, cont3. And nested template should be hide one specific control for cont1 only. Before inclusion into cont1 I would like to assign value to some flag variable $hideMyControl. And…
sergtk
  • 10,714
  • 15
  • 75
  • 130
34
votes
4 answers

Velocity string function

I just start using Java Velocity. Now I want to create a java class template. package $app.package_namespace public class ${app.name}Station { #foreach($s_attribute in $app.station) $s_attribute.type $s_attribute.name, #end …
Yiming
  • 383
  • 1
  • 4
  • 6
34
votes
8 answers

JSP vs Velocity what is better?

What is better between JSP and velocity in - Performance - Ease of use - Ease of creating reusable components - Availability of open source 3rd parties - IDE support
flybywire
  • 261,858
  • 191
  • 397
  • 503
33
votes
2 answers

velocity (test instanceof)

How can I test the class of a given object in a velocity template. I can't find an instanceof directive
Amira
  • 331
  • 1
  • 3
  • 3
33
votes
3 answers

Velocity nulls and empty strings

In velocity I have a variable which its value is null. I don't want to display anything in that case. Currently the template engine translates "" to null so I have to do. #set ( $a = "") #if ($a) assert("never prints a neither gets here: " +…
Jordi P.S.
  • 3,838
  • 7
  • 36
  • 59
32
votes
3 answers

How to use 'for' loop in velocity template?

I just googled for 'for loop', but it looks like velocity has 'foreach' only. How do I use 'for loop' in velocity template?
Moon
  • 22,195
  • 68
  • 188
  • 269
32
votes
5 answers

Problem with subject encoding when sending an email

I'm sending an email and I'm receiving it correctly but the encoding of the subject is not correct. I'm sending "invitación" but I'm receiving "invitaci?n". The content of the message is OK. The content of the message is coming from a…
Javi
  • 19,387
  • 30
  • 102
  • 135
32
votes
2 answers

What are the main differences between StringTemplate and FreeMarker?

The net seems to be full of comparisons between Velocity and FreeMarker, and they seem to be reasonably equivalent. But there seems to be almost no comparisons between StringTemplate and FreeMarker! So, what are the main differences between…
Nakedible
  • 4,067
  • 7
  • 34
  • 40
30
votes
4 answers

velocity: do something except in last loop iteration

In velocity, I want to do something different in the last loop. What is the correct idiom? RELATED: Last iteration of enhanced for loop in java
flybywire
  • 261,858
  • 191
  • 397
  • 503
28
votes
2 answers

Escaping quotes in velocity template

I have a java method, that takes a few strings. This method needs to be called from a Velocity Template. However, the strings are too complex, with lots of single quotes, double quotes and commas as well. As a result merge is failing. Is there a way…
Krishna Sarma
  • 1,852
  • 2
  • 29
  • 52
26
votes
5 answers

Why should I learn and use struts?

I'm a java developer, not seasoned, but I am familiar with most concepts reasonably well. I recently built a website using Tomcat/JSP (~30 dynamic pages). I made the newbie mistake of including large sections of core logic in the JSP, using the…
David Parks
  • 30,789
  • 47
  • 185
  • 328
26
votes
2 answers

Calling class methods (static) from inside a velocity view page

Can you call class methods from inside a view page? Specifically ones that are not passed into the view? In asp.net MVC I can do this: <%= SomeClass.FixDateFormat(ViewData.Model.SomeClass.DateCreated) %>
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
25
votes
2 answers

Apache Velocity foreach loop #continue

Is there a #continue command for jump to next iteration in foreach loop?
Premier
  • 4,160
  • 6
  • 44
  • 58