0

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

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); 
4

1 回答 1

2

您的模板引擎配置了 ServletContextTemplateResolver 而不是FileTemplateResolverClassLoaderTemplateResolver。这很可能在某个地方的 spring 配置文件中定义。如果在代码中配置,请参阅Thymeleaf 用户指南配置模板引擎配置 templateResolver。这是个好医生。

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

<beans:bean id="templateResolver"
    class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
</beans:bean>
于 2013-06-05T15:51:36.207 回答