在Spring Scheduler中使用thymeleaf发送邮件

时间:2013-05-31 07:19:03

标签: spring-mvc thymeleaf

我正在尝试使用百日咳模板发送电子邮件。但我收到一条错误消息

org.thymeleaf.exceptions.TemplateProcessingException: Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]
    at org.thymeleaf.resourceresolver.ServletContextResourceResolver.getResourceAsStream(ServletContextResourceResolver.java:74)
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:221)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1192)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1148)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1095)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1008)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:982)
    at in.coep.vlabteam.leap.implementations.ScheduleNotificationImpl.sendNotification(ScheduleNotificationImpl.java:205)
    at in.coep.vlabteam.leap.implementations.ScheduleNotificationImpl.sendScheduleNotificationMail(ScheduleNotificationImpl.java:105)
    at in.coep.vlabteam.leap.services.ScheduleNotificationService.sendScheduleNotificationByMail(ScheduleNotificationService.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
10936 [taskScheduler-1] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][taskScheduler-1] Exception processing template "scheduleMail.html": Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]

我不能使用WebContext()而不是Context()。因为对于webContext我需要HttpServletRequest对象,我无法在这里获取,因为它不在请求范围内。 我试图使用context()发送邮件,但我收到一个错误。 请有人对此有解决方案。提前致谢 这是我的代码,

final Context ctx = new Context(); 
    ctx.setVariable("eagletId", user.getEagletId());
    ctx.setVariable("name", user.getFirstName());
    ctx.setVariable("setSentDate", new Date());
    ctx.setVariable("department", user.getDepartment());
    ctx.setVariable("batch", user.getBatch());
    // ctx.setVariable("month" Constants.LeapConstants.UserType);

    // Prepare message using a Spring helper 
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); 
    final MimeMessageHelper message =  
            new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); 
    message.setSubject("Create your report for month"); 
    message.setFrom("leap@gmail.com"); 
    message.setTo("vlab@gmail.com"); 

    // Create the HTML body using Thymeleaf 
    final String htmlContent = this.templateEngine.process("scheduleMail.html", ctx); 
    message.setText(htmlContent, true /* isHtml */); 

    // Send mail 
    this.mailSender.send(mimeMessage); 

1 个答案:

答案 0 :(得分:2)

您的模板引擎配置了ServletContextTemplateResolver,而不是FileTemplateResolverClassLoaderTemplateResolver。这将在某个地方的spring配置文件中定义。如果在代码中配置,请参阅Thymeleaf user's guide on configuring the template engineconfiguration of the templateResolver。这是个好文档。

通过xml配置,它看起来像这样:

<beans:bean id="templateResolver"
    class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
</beans:bean>