Velocity文件夹模板

时间:2013-02-19 17:22:46

标签: spring-mvc javamail velocity

我正在尝试使用速度框架通过我的webapp发送电子邮件。我已经用这种方式配置了我的email-context.xml

..............    
<bean id="velocityEngine"
            class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
            <property name="velocityProperties">
                <props>
                    <prop key="resource.loader">file</prop>
                    <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
                    <prop key="file.resource.loader.path">/WEB-INF/velocity/</prop>
                </props>
            </property>
        </bean>

        <bean id="mailService" class="--.------.-----.service.MailServiceImpl">
            <property name="mailSender" ref="mailSender" />
            <property name="velocityEngine" ref="velocityEngine" />
        </bean>

然后我像这样编码了我的mailServiceImpl类

public class MailServiceImpl implements MailService {

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;


    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

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

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

    ........................

    public void sendEmail(final SimpleMailMessage msg, final Map<Object, Object> map) throws MessagingException {
        System.out.println("Sending mail...");
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                message.setTo(msg.getTo());
                message.setFrom(msg.getFrom());
                message.setSubject(msg.getSubject());

                String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "invite.vm", map);

                message.setText(body, true);
            }
        };
        mailSender.send(preparator);
    }
...........

当我在上面的方法上启动测试时,我得到了这个异常

........
19 feb 2013 17:13:46 TRACE [main] TransactionAspectSupport - Completing transaction for [it.stasbranger.spidly.service.MailServiceImpl.sendEmail] after exception: org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'invite.vm'
19 feb 2013 17:13:46 TRACE [main] RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'invite.vm'
19 feb 2013 17:13:46 TRACE [main] RuleBasedTransactionAttribute - Winning rollback rule is: null
...........

有什么建议吗?

0 个答案:

没有答案