tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Template Engines > Velocity > Properties

Properties 

Apache Velocity is a free, simple and powerful template engine written in 100% pure Java. This requires the libraries velocity-1.7.jar, oro-2.0.8.jar, commons-lang-2.4.jar, commons-collections-3.2.1.jar, commons-logging-1.1.jar, log4j-1.2.12.jar to be in classpath. The following example shows accessing bean properties in Velocity.

File Name  :  
/VELOCITY001/config/props.vm 

File Name  :  
com/bethecoder/tutorials/velocity/common/ComplexBean.java 
   
package com.bethecoder.tutorials.velocity.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ComplexBean {

  private List<Student> studentList = new ArrayList<Student>();
  private Map<String, List<Student>> subjectStudentMap = new HashMap<String, List<Student>>();
  
  public List<Student> getStudentList() {
    return studentList;
  }
  public void setStudentList(List<Student> studentList) {
    this.studentList = studentList;
  }
  public Map<String, List<Student>> getSubjectStudentMap() {
    return subjectStudentMap;
  }
  public void setSubjectStudentMap(Map<String, List<Student>> subjectStudentMap) {
    this.subjectStudentMap = subjectStudentMap;
  }
  
  public Set<String> getSubjects() {
    return subjectStudentMap.keySet();
  }
}
   

File Name  :  
com/bethecoder/tutorials/velocity/common/Student.java 
   
package com.bethecoder.tutorials.velocity.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/velocity/tests/PropertiesTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.velocity.tests;

import java.io.StringWriter;
import java.util.Arrays;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import com.bethecoder.tutorials.velocity.common.ComplexBean;
import com.bethecoder.tutorials.velocity.common.Student;

public class PropertiesTest {

  /**
   @param args
   */
  public static void main(String[] args)  {

    /**
     * Initialize engine and get template
     */
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath")
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        Template template = ve.getTemplate("props.vm");
        
        /**
         * Prepare context data
         */
        Student student = new Student("Sriram"2"Chess");
    Student student2 = new Student("Sudhakar"29"Painting");
    Student student3 = new Student("Anu"28"Reading books");
    
    ComplexBean complexBean = new ComplexBean();
    complexBean.getStudentList().addAll(Arrays.asList(student, student2, student3));
    complexBean.getSubjectStudentMap().put("MATHS"
        Arrays.asList(student, student2, student3));
    
    complexBean.getSubjectStudentMap().put("PHYSICS"
        Arrays.asList(student, student2));
    
        VelocityContext context = new VelocityContext();
        context.put("bean", complexBean);

        /**
         * Merge data and template
         */
        StringWriter swOut = new StringWriter();
        template.merge(context, swOut);
        
        System.out.println(swOut);
  }
}
   

It gives the following output,
Students :  Sriram   Sudhakar   Anu  
Subjects :  MATHS   PHYSICS  

Math Students :  Sriram   Sudhakar   Anu  
Physics Students :  Sriram   Sudhakar  



 
  


  
bl  br