How to use speed 1.7 using Spring

I use withe spring 3.1 speed 1.7 to send email. speed is used for email templates.

Below is the configuration

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

and below is my code

 @Component
 public class EmailUtils {

    @Autowired
    private static VelocityEngine velocityEngine;

    public static void sendMail(String subject, Map data, String template,
            String toName, String toAddress) {


        HtmlEmail email = new HtmlEmail();

        try {
            email.setHostName(hostName);
            email.setSmtpPort(smtpPort);
            email.setSubject(subject);

            System.out.println(template +" template");
            System.out.println(data +" data ");
            System.out.println(velocityEngine +" velocityEngine ");

            String message = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, template, data);

            System.out.println(message +" message message ");

            email.setMsg(message);
            email.addTo(toAddress, toName);
            email.setFrom(fromAddress, fromName);
            email.send();
        } catch (EmailException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
    }
}

When I run the application, I get the following error.

java.lang.NullPointerException
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)

when the speed motor is zero.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

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

Please help me. Is there any other configuration I need to do?

+5
source share
3 answers

I had the same issue when using. I could solve it like this:

@Configuration
public class ApplicationConfiguration {

    @Bean
    public VelocityEngine getVelocityEngine() throws VelocityException, IOException{
        VelocityEngineFactory factory = new VelocityEngineFactory();
        Properties props = new Properties();
        props.put("resource.loader", "class");
        props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        factory.setVelocityProperties(props);
        return factory.createVelocityEngine();      
    }
}

But instead - because I used speed templates for my views and already declared VelocityConfigurer, so instead I got Autowired in my VelocityConfigurer declaration and called getVelocityEngine ().

, @Service-s @Component-s applicationContext.xml, xml applicationContext.xml xxx-servlet.xml config.

, , applicationContext.xml , xxx-servlet.xml.

spring bean factory , - :

log4j.category.org.springframework.beans.factory=DEBUG
+8

, @Autowired . :

@Autowired
public void setVelocityEngine(VelocityEngine ve) {
  EmailUtils.velocityEngine = ve;
}

-, EmailUtils bean. Spring ( ), , DI philosphy.

+5

This is my attempt, I initialize the "speedEngine" bean manually through ClassPas thanksmlApplicationContext:

Spring application-context.xml

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  <property name="velocityProperties">
    <value>
      resource.loader=class
      class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
    </value>
  </property>
</bean>

This is my template_text.vm, put it in the classpath at the same level as application-context.xml.

Dear $userName,

You have made the following request on $userTime.

#if( $isFileMissing )
Missing file(s) found in home directory:
#foreach( $missingFile in $missingFiles )
- $missingFile
#end
#end

Best regards,

This is the Java code:

public static void main(String[] args) throws Exception {
    String[] springConfig = {"application-context.xml"};
    org.springframework.context.ApplicationContext context = new org.springframework.context.support.ClassPathXmlApplicationContext(springConfig);

    java.util.Map<String, Object> model = new java.util.HashMap<String, Object>();
    model.put("userName", "John Low");
    model.put("userTime", "2015-10-28 23:59:59");
    model.put("isFileMissing", true);
    model.put("missingFiles", new String[] {"line 1", "line 2", "line 3"});
    String text = org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString((VelocityEngine)context.getBean("velocityEngine"), "/template_text.vm", "UTF-8", model);
    System.out.println(text);
}

Output:

Dear John Low,

You have made the following request on 2015-10-28 23:59:59.

Missing file(s) found in home directory:
- file 1
- file AAA
- line 3

Best regards,
+1
source

All Articles