Hi,
It’s my first post of this year, after my first post concerning the library Apache Velocity Simple Presentation of Velocity, I would expose you an article about simple example of use of Velocity. A memo to use speedly Velocity.
First, our project needs the following jars to the classpath of your project:
- velocity-1.7.jar
- velocity-tools-2.0.jar
It is necessary to initialize Velocity, via the method Velocity.init(…) which allow to set the properties. To target this goal, we create a singleton class named VelocityInitializer:
02 | * Singleton that initializes velocity (see Apache Velocity project) |
07 | public class VelocityInitializer { |
09 | private boolean isInitialized = false ; |
13 | public static VelocityInitializer getInstance(){ |
14 | return SingletonHolder.instance; |
18 | private static class SingletonHolder{ |
19 | private static final VelocityInitializer instance = new VelocityInitializer(); |
23 | private VelocityInitializer(){ } |
28 | * Initialize Velocity (template engine library) |
30 | public void initializeVelocity(){ |
34 | Properties velocityProps = new Properties(); |
35 | velocityProps.setProperty( "resource.loader" , "class" ); |
37 | velocityProps.setProperty( "class.resource.loader.class" , "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" ); |
38 | velocityProps.setProperty( "runtime.log" , "" ); |
39 | velocityProps.setProperty( "directive.foreach.counter.initial.value" , "0" ); |
41 | Velocity.init(velocityProps); |
43 | throw new RuntimeException( "Internal error : impossible to initialize velocity." ,e); |
Then, this initialization of Velocity must be done – in a Spring-based application – after the loading of Spring context. For example, we could implement the ApplicationContextAware interface:
01 | public class ContextUtil implements ApplicationContextAware{ |
03 | private static final Logger log = Logger.getLogger(ContextUtil. class ); |
05 | private static ApplicationContext context; |
08 | public void setApplicationContext(ApplicationContext context) throws BeansException{ |
09 | ContextUtil.context = context; |
10 | log.info( "Spring initialized" ); |
13 | VelocityInitializer.getInstance().initializeVelocity(); |
16 | public static ApplicationContext getContext(){ |
20 | @SuppressWarnings ( "unchecked" ) |
21 | public static <T> getBean(String name){ |
22 | return (T) context.getBean(name); |
To use the Velocity templates, we create a Singleton VelocityUtil allowing the merge of template and data:
01 | public class VelocityUtil { |
03 | private VelocityUtil(){} |
05 | private static VelocityUtil instance; |
08 | public static VelocityUtil getInstance(){ |
10 | instance = new VelocityUtil(); |
16 | public void render(String templatePath, Map<String, Object> context, Writer writer){ |
18 | VelocityContext velocityContext = new VelocityContext(context); |
19 | Template template = null ; |
21 | template = Velocity.getTemplate(templatePath); |
23 | throw new RuntimeException( "Internal error : impossible to load the Velocity template: " + templatePath, e); |
25 | template.merge(velocityContext, writer); |
26 | } catch (Throwable th) { |
32 | public CharSequence render(String templatePath, Map<String, Object> context){ |
33 | StringWriter writer = new StringWriter( 1024 ); |
34 | render(templatePath,context, writer); |
35 | CharSequence ret = writer.getBuffer(); |
So, for example, below a Template:
2 | It's a test of Apache Velocity on JAVABLOG.FR website. |
3 | #if ($myobj.code == "ID") |
4 | The object ID is $!{myobj.objectID}. |
6 | The object name is $!{myobj.objectName}. |
…and a POJO class used for our example:
01 | public class MyObject { |
03 | private String objectName; |
04 | private String objectID; |
14 | public String getCode() { |
… and finally, the main class or JUNIT will be:
01 | public class MainTest { |
03 | private static final String TEMPLATE = "/src/com/ho/apache/velocity/test1/test1.vm" ; |
05 | public static void main(String[] args) { |
07 | final Map<String, Object> context = new HashMap<String, Object>(); |
08 | MyObject aObject = new MyObject(); |
09 | aObject.setObjectID( "Object id" ); |
10 | aObject.setObjectName( "Object name" ); |
11 | aObject.setCode( "ID" ); |
12 | aObject.setLabel( "Object created for Velocity tests" ); |
14 | context.put( "myobj" , aObject); |
15 | context.put( "name" , "ozveren" ); |
16 | context.put( "firstname" , "huseyin" ); |
18 | CharSequence result = VelocityUtil.getInstance().render(TEMPLATE, context); |
19 | String resultStr = result.toString(); |
20 | System.out.println( "Result :" ); |
21 | System.out.println(resultStr); |
23 | System.out.println( "-------------------------------" ); |
25 | final Map<String, Object> context = new HashMap<String, Object>(); |
26 | MyObject aObject = new MyObject(); |
27 | aObject.setObjectID( "Object id" ); |
28 | aObject.setObjectName( "Object name" ); |
29 | aObject.setCode( "NAME" ); |
30 | aObject.setLabel( "Object created for Velocity tests" ); |
32 | context.put( "myobj" , aObject); |
33 | context.put( "name" , "ozveren" ); |
34 | context.put( "firstname" , "huseyin" ); |
36 | CharSequence result = VelocityUtil.getInstance().render(TEMPLATE, context); |
37 | String resultStr = result.toString(); |
38 | System.out.println( "Result :" ); |
39 | System.out.println(resultStr); |
The outputs are:
2 | It's a test of Apache Velocity on JAVABLOG.FR website. |
3 | The object ID is Object id. |
3 | It's a test of Apache Velocity on JAVABLOG.FR website. |
4 | The object name is Object name. |
That’s all!!!!
Huseyin OZVEREN
Source : velocity-test1.zip
Related