Tuesday, November 26, 2019

Velocity in Spring boot - focus on velocity template vm file path


There are 2 cases: depend on where your velocity template .vm file is placed.

1. If the .vm file is not placed in the resources directory:

> project
       > src/main/java
       > src/main/resources
       > prjconfig
               > emailTemplate
             > order.vm

In @Configuration class:

@Bean
public VelocityEngine velocityEngine() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,                                                                                                                                "./prjconfig/emailTemplate");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

In service class:

        @Autowired
private VelocityEngine velocityEngine;

        private String prepareRegistrationEmailText(List<OrderData> list) {
VelocityContext context = new VelocityContext();
context.put("alertList", list);
Writer stringWriter = new StringWriter();
                // only file name
velocityEngine.mergeTemplate("order.vm", "UTF-8", context, stringWriter);
String text = stringWriter.toString();
return text;
}

I have tried with "classpath" but didn't work :)

2. If the .vm file is placed in the resources directory:

> project
       > src/main/java
       > src/main/resources    
                > emailTemplate
                 > order.vm 

In @Configuration class:

       @Bean
public VelocityEngine velocityEngineClasspath() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

In service class:

        @Autowired
private VelocityEngine velocityEngine;

        private String prepareRegistrationEmailText(List<OrderData> list) {
VelocityContext context = new VelocityContext();
context.put("alertList", list);
Writer stringWriter = new StringWriter();
                // full path of file
velocityEngine.mergeTemplate("/emailTemplate/order.vm", "UTF-8", context, stringWriter);
return stringWriter.toString();
}



Maven dependency:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
There is no need for spring-support dependency.

Cheer (^.^)
theroad


No comments:

Post a Comment