Apache Velocity Engine Documentation

repository·master·Indexed 19 days ago

https://github.com/apache/velocity-engine

A general-purpose Java-based template engine used to generate text-based outputs like HTML, email, or source code using the Velocity Template Language (VTL). Includes modules for the core engine, JSR-223 scripting, and Spring integration support for both modern Spring (6.x/7.x on Java 17) and legacy Spring 5.x (on Java 8). Provides guidance on customizing VTL parser syntax and various usage examples including standalone applications and database-backed contexts.

Tokens
1.9K
Snippets
8
Records
14
Agent score
64%

What's inside Apache Velocity

  1. Overview of Apache Velocity Engine modules

    master

    Apache Velocity is a general-purpose template engine written in Java. The repository is organized into several functional modules:

    • velocity-engine-core/: The core engine module.
    • velocity-engine-examples/: Simple usage examples for Java applications.
    • velocity-engine-scripting/: A JSR-223 implementation for using Velocity as a scripting language.
    • spring-velocity-support: A Velocity Engine factory bean designed for modern Spring (6.x/7.x) running on Java 17.
    • spring5-velocity-support: A Velocity Engine factory bean for legacy Spring 5.x running on Java 8.
  2. How to customize the VTL parser syntax

    master

    You can build a custom parser to change the characters used by the Velocity Template Language (VTL) syntax. This is useful when your templates contain characters that conflict with default VTL delimiters like *, @, $ or # (for example, when embedding jQuery code that uses $).

    For detailed implementation instructions, refer to the Velocity Developer Guide.

  3. Integrate Apache Velocity with Spring 6.x or 7.x

    master

    Use the spring-velocity-support module to integrate Apache Velocity into modern Spring applications. This module is Jakarta-based and requires Java 17+. It is compatible with both Spring 6.x and Spring 7.x because it relies on stable spring-core, spring-beans, and spring-context APIs.

    If you are working on a legacy project using Spring 5.x on Java 8, do not use this module; instead, use the spring5-velocity-support module.

    <bean id="velocityEngine"
        class="org.apache.velocity.spring.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loaders">classpath</prop>
                <prop key="resource.loader.classpath.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
            </props>
        </property>
    </bean>
  4. Try Velocity usage examples

    master
    After building the Velocity Engine, you can build the included examples to see how to integrate Velocity into your Java applications. Detailed instructions and code samples are located in the velocity-engine-examples directory.
  5. Configure VelocityEngineFactoryBean for Spring 5.x

    master

    To use Apache Velocity within a Spring 5.x application (running on Java 8), configure the org.apache.velocity.spring.VelocityEngineFactoryBean as a bean in your Spring XML configuration. This allows you to define engine properties, such as resource loaders, directly within the bean definition.

    Note: This module is intended for legacy Spring 5.x environments. For modern Spring (6.x/7.x) on Java 17, use the spring-velocity-support module instead.

    <bean id="velocityEngine"
        class="org.apache.velocity.spring.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loaders">classpath</prop>
                <prop key="resource.loader.classpath.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
            </props>
        </property>
    </bean>
  6. Run Velocity examples

    master

    After downloading or building the ${project.build.finalName}-pkg.zip package, unzip it and navigate to the ${project.build.finalName} directory.

    Note for Windows users: The provided .sh shell scripts are designed for Linux or BSD. To run them on Windows, you must create batch files that construct the classpath using the JAR files in the lib/ directory and then invoke java on the target main class with the required arguments.

  7. Build Velocity examples from source

    master
    To build the examples from the Velocity source code, follow the instructions on the official Velocity build page. The build process generates a zip archive located at target/${project.build.finalName}-pkg.zip. This archive contains all the examples and a build.sh script, which can be used to re-build the examples after making changes to the source files in the src directory.
  8. Configure VelocityEngineFactoryBean in Spring

    master

    To set up the Velocity engine within a Spring application context, define a bean of type org.apache.velocity.spring.VelocityEngineFactoryBean. You can pass custom Velocity configuration properties via the velocityProperties property using a standard Spring <props> element.

    For example, to configure the ClasspathResourceLoader, set resource.loaders to classpath and specify the loader class using resource.loader.classpath.class.

    <bean id="velocityEngine"
        class="org.apache.velocity.spring.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loaders">classpath</prop>
                <prop key="resource.loader.classpath.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
            </props>
        </property>
    </bean>