Home | FAQ | Contact me

Apache Velocity

Russell Bateman
March 2016
last update:

From The Apache Velocity Project.

Here's a more complex example than all the hello world tutorials for Apache Velocity. It was suggested by Apache's page (see mud-index.vm), but which Apache did not see fit to finish.

The source template

mud-index.vm:

Highlighted are Velocity Template Language (VTL) statements. The rest is just HTML since that's what we want to use the output for.

<html>
<body>
Hello $customer.name!
<table>
#foreach( $mud in $mudsOnSpecial )
   #if ( $customer.hasPurchased( $mud ) )
      <tr>
        <td>
          $flogger.getPromo( $mud )
        </td>
      </tr>
   #end
#end
</table>
</body>
</html>

How to code (in Java) to Apache Velocity

MudStore.java:
package com.etretatlogiciels;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class MudStore
{
  public static void main( String[] args ) throws Exception
  {
    // initialize engine...
    VelocityEngine ve = new VelocityEngine();
    ve.init();

    // get the template...
    Template t = ve.getTemplate( new StandardResource( "mud-index.vm" ).resourcePath() );

    // create inventory...
    List< Mud > mudsOnSpecial = new ArrayList<>();
    mudsOnSpecial.add( new Mud( "Bentonite clay" ) );
    mudsOnSpecial.add( new Mud( "Swamp mud" ) );
    mudsOnSpecial.add( new Mud( "Slippery mud" ) );
    mudsOnSpecial.add( new Mud( "Gooey mud" ) );

    // create customer data...
    Customer customer = new Customer();
    customer.setNumber( "099" );
    customer.setName( "Charlie Brown" );
    customer.setAddress( "1770 James Street" );
    customer.setCity( "Minneapolis" );
    customer.setState( "Minnesota" );
    customer.setZip( "55401" );
    customer.setPhone( "612 555-1234" );
    customer.addMudPurchase( new Mud( "Bentonite clay" ) );

    // create flogger...
    Flogger flogger = new Flogger( "Earl Scheib will deliver %s for $49.95!" );

    // create context and add data...
    VelocityContext context = new VelocityContext();
    context.put( "customer", customer );
    context.put( "mudsOnSpecial", mudsOnSpecial );
    context.put( "flogger", flogger );

    // render template...
    StringWriter writer = new StringWriter();
    t.merge( context, writer );

    // show what got done...
    System.out.println( writer.toString() );
  }
}

Supporting Java code for the above

Mud.java:
package com.etretatlogiciels;

public class Mud
{
  private String kind;

  public Mud( String kind ) { this.kind = kind; }

  public String getKind() { return this.kind; }
}
Customer.java:
package com.etretatlogiciels;

import java.util.ArrayList;
import java.util.List;

public class Customer
{
  private String number;
  private String name;
  private String address;
  private String city;
  private String state;
  private String zip;
  private String phone;
  private List< Mud > mudsPurchased = new ArrayList<>();

  public String getNumber()  { return number; }
  public String getName()    { return name; }
  public String getAddress() { return address; }
  public String getCity()    { return city; }
  public String getState()   { return state; }
  public String getZip()     { return zip; }
  public String getPhone()   { return phone; }

  public boolean hasPurchased( Mud mud )
  {
    String proposed = mud.getKind();

    for( Mud m : mudsPurchased )
    {
      if( proposed.equals( m.getKind() ) )
        return true;
    }

    return false;
  }

  public void setNumber     ( String number )  { this.number = number; }
  public void setName       ( String name )    { this.name = name; }
  public void setAddress    ( String address ) { this.address = address; }
  public void setCity       ( String city )    { this.city = city; }
  public void setState      ( String state )   { this.state = state; }
  public void setZip        ( String zip )     { this.zip = zip; }
  public void setPhone      ( String phone )   { this.phone = phone; }
  public void addMudPurchase( Mud mud )        { this.mudsPurchased.add( mud ); }
}
Flogger.java:
package com.etretatlogiciels;

public class Flogger
{
  private String promo;

  public Flogger( String promo ) { this.promo = promo; }

  public String getPromo( Mud mud ) { return String.format( this.promo, mud.getKind() ); }
}
StandardResource.java:
package com.etretatlogicials;

public class StandardResource
{
  private String name;

  private static final String RESOURCES = "./src/main/resources";

  public StandardResource( String resourceName ) { this.name = resourceName; }
  public String resourcePath() { return RESOURCES + this.name; }
}

Output

This doesn't come out pretty—you can blame Apache.

<html>
<body>
Hello Charlie Brown!
<table>
          <tr>
         <td>
           Earl Scheib will deliver Bentonite clay for $49.95!
         </td>
       </tr>
            </table>
</body>
</html>