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
141
votes
12 answers

String replacement in java, similar to a velocity template

Is there any String replacement mechanism in Java, where I can pass objects with a text, and it replaces the string as it occurs? For example, the text is: Hello ${user.name}, Welcome to ${site.name}. The objects I have are user and site. I want…
Sastrija
  • 3,284
  • 6
  • 47
  • 64
94
votes
7 answers

Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh

I'm about to choose to way to organize my view (with spring-mvc, but that shouldn't matter much) There are 6 options as far as I see (though they are not mutually exclusive): Tiles Sitemesh Freemarker Velocity <%@ include…
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
76
votes
5 answers

Velocity vs. FreeMarker

Velocity or FreeMarker? They look pretty much the same, even the syntax? What to use? Or when to use what?
flybywire
  • 261,858
  • 191
  • 397
  • 503
66
votes
7 answers

How to use String as Velocity Template?

What is the best way to create Velocity Template from a String? I'm aware of Velocity.evaluate method where I can pass String or StringReader, but I'm curios is there a better way to do it (e.g. any advantage of creating an instance of Template).
tomsame
  • 810
  • 1
  • 7
  • 10
63
votes
14 answers

Unable to find velocity template resources

Just a simple velocity standalone app based on maven structure. Here is the code snippet written in Scala to render the template helloworld.vm in ${basedir}/src/main/resources folder: com.ggd543.velocitydemo import…
Archer
  • 631
  • 1
  • 5
  • 4
59
votes
6 answers

How to escape a # in velocity

I would like to know how can i escape a # in velocity. Backslash seems to escape it but it prints itself as well This: \#\# prints: \#\# I would like: ##
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
56
votes
2 answers

$null check in velocity

I came to know about null check using $null in velocity 1.6 through a resource updated by you. Resource: Reading model objects mapped in Velocity Templates But I am facing so many challenges that there is no $null for null check in velocity as there…
lucky
  • 561
  • 1
  • 4
  • 3
54
votes
4 answers

in velocity can you iterate through a java hashmap's entry set()?

Can you do something like this in a velocity template? #set ($map = $myobject.getMap() ) #foreach ($mapEntry in $map.entrySet()) $mapEntry.key() $mapEntry.value() #end it outputs blank tags like so:
Ayrad
  • 3,996
  • 8
  • 45
  • 86
52
votes
5 answers

Loading velocity template inside a jar file

I have a project where I want to load a velocity template to complete it with parameters. The whole application is packaged as a jar file. What I initially thought of doing was this: VelocityEngine ve = new VelocityEngine(); URL url =…
Rafael
  • 2,373
  • 4
  • 22
  • 28
49
votes
4 answers

How to convert string into integer in the Velocity template?

I have a Velocity template file which has the data from XML. I want to convert the string into integer type. How can I do that?
uma
  • 529
  • 1
  • 5
  • 7
46
votes
2 answers

How I hide empty Velocity variable names?

I am using Struts + Velocity in a Java application, but after I submit a form, the confirmation page (Velocity template) shows the variable names instead an empty label, like the Age in following example: Name: Fernando Age: {person.age} Sex:…
Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51
43
votes
1 answer

Replace a Substring of a String in Velocity Template Language

I want to replace a part of a string in Velocity Template Language with another string. For Example: #set($a = "Hello") #set($b = "+") I want to replace ll in Hello with ++. The output should be He++o Please help me Thanks Kishore
kishore
  • 431
  • 1
  • 4
  • 4
42
votes
3 answers

How to access/get the size of an array/collection in velocity templates?

I am using velocity for email templates in my java/spring 3 app. How could I get the size of an ArrayList added to the model from within the template.
Danny
  • 1,291
  • 4
  • 13
  • 16
42
votes
3 answers

How to do an inline if/otherwise (aka ternary operator) in Velocity?

In pure Java, I could do this: value = (a > b) ? a : b; Whereas in Velocity, the long form would be: #if($a > $b) #set($value = $a) #else #set($value = $b) #end Is there a short form in Velocity? I want to be able to do an…
Michael
  • 7,348
  • 10
  • 49
  • 86
40
votes
3 answers

"for" loop in velocity template

I already posted a similar question a week ago on How to use 'for' loop in velocity template?. So...basically I can't use 'for' loop in a velocity template. Let's say I have a variable that holds integer 4. I want to display something four times…
Moon
  • 22,195
  • 68
  • 188
  • 269
1
2 3
99 100