速度零点异常 [英] velocity null point exception

查看:92
本文介绍了速度零点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用速度模板发送邮件。

I want to send mail using velocity templates.

我的配置基于Spring 3.1文档。

My configuration based on Spring 3.1 documentation.

我有一个配置的xml文件:

I have an xml file with configuration:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd >

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">






<bean id="sendMail" class="com.myclass.app.SendMail">
    <property name="mailSender" ref="mailSender"/>
    <property name="velocityEngine" ref="velocityEngine"/>
</bean>

<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>

然后我上课了:

@Controller
public class SendMail{


    private static final Logger logger = Logger.getLogger(SendMail.class);

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }


    @RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
    public @ResponseBody void send(){

        sentEmail();

    }

    private void sentEmail(){

        try {


//            SimpleMailMessage msg = new SimpleMailMessage(mailMessage);

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,
                    true, "UTF-8");
            helper.setFrom("from@from.com");
            helper.setTo("myEmail");

            helper.setSubject("title");

            Map map = new HashMap();
            map.put("user", "Piotr");
            map.put("test", "TEST");

            String text1 = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "velocity.vm", map );

            logger.info(text1);

            String text = "test";

                helper.setText(text, true);


            mailSender.send(message);

        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
            System.out.println(ex.getMessage());
        }


    }

}

我的vm文件位于resources文件夹中。

my vm file is located in "resources" folder.

我收到的所有内容都是空点异常;

And all i receive id "Null point Exception";

该回复不再给我任何信息。

The response doesn't give me nothing more.

您有什么想法,该怎么办?

Do you have any ideas, what to do ?

推荐答案

您的模板不能在资源中,它们需要位于您可以在applicationContext.xml中的velocityEngine
中配置的类路径中:

Your templates can't be in resources, they need to be in the classpath that you can configure in the velocityEngine in your applicationContext.xml :

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="ADDRESS_OF_YOUR_SMTP_SERVER"/>
  <property name="defaultEncoding" value="UTF-8"/>
  <property name="port" value="XX_PORT_SERVER"/>
  <property name="javaMailProperties">  
      <props>
        <prop key="mail.smtps.auth">false</prop> <!-- depending on your smtp server -->
        <prop key="mail.smtp.starttls.enable">false</prop>
      </props>
  </property>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" p:resourceLoaderPath="classpath:META-INF/velocity" />

不要忘记命名空间xmlns:p =http://www.springframework.org/ schema / p使用上面例子的属性。

Don't forget the namespace xmlns:p="http://www.springframework.org/schema/p" to use the properties of the above example.

在你的Java类SendMail中,不要忘记添加注释@Autowired:

In your Java class SendMail, don't forget to add the annotation @Autowired :

@Autowired
public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
}

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

最后,要发送电子邮件,请使用MimeMessagePreparator:

and finally, to send your email, use the MimeMessagePreparator:

            MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
               MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
               message.setTo("blabla@test.com");
               message.setSubject("Email test");

               String text = VelocityEngineUtils.mergeTemplateIntoString(
                   velocityEngine, template, keywords);
               message.setText(text, true);

             }
          };

          //Send email using the autowired mailSender
          this.mailSender.send(preparator);

这篇关于速度零点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆