Java ServletContextResourceResolver只能在上下文实现时执行。IWebContext[当前上下文:..上下文]…错误

Java ServletContextResourceResolver只能在上下文实现时执行。IWebContext[当前上下文:..上下文]…错误,java,spring,thymeleaf,Java,Spring,Thymeleaf,我试图用Spring和ThymeLeaf发送一封HTML电子邮件,但我收到了这个错误。请帮我解决它 [THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager [THYMELEAF] * Template modes: [THYMELEAF] * HTML5 [THYMELEAF] * VALIDXHTML [THYMELEAF] * XHTML [THYMELE

我试图用Spring和ThymeLeaf发送一封HTML电子邮件,但我收到了这个错误。请帮我解决它

[THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager
[THYMELEAF] * Template modes:
[THYMELEAF]     * HTML5
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * XHTML
[THYMELEAF]     * VALIDXML
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * XML
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * [1] org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
[THYMELEAF]     * [2] org.thymeleaf.templateresolver.ServletContextTemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF]     * org.thymeleaf.spring3.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect: org.thymeleaf.spring3.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
15544 [JavaFX Application Thread] INFO org.thymeleaf.TemplateEngine - [THYMELEAF] TEMPLATE ENGINE INITIALIZED
15558 [JavaFX Application Thread] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][JavaFX Application Thread] Exception processing template "email-inlineimage.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]
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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:78)
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:221)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)
    at wakiliproject.ServerSide.SpringThymeLeaf.ThymeEmailService.sendMailWithInline(ThymeEmailService.java:51)
我的xml配置如下:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />

    <!-- THYMELEAF: Template Resolver for email templates -->
    <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="templateMode" value=".html" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="order" value="1" />
    </bean>

    <!-- THYMELEAF: Template Resolver for webapp pages   -->
    <!-- (we would not need this if our app was not web) -->
    <bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="templateMode" value=".html" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="order" value="2" />
    </bean>

    <bean id="templateEngineCustom" class="org.thymeleaf.spring3.SpringTemplateEngine">
        <property name="templateResolvers">
            <set>
                <ref bean="emailTemplateResolver" />
            </set>
        </property>
    </bean>

    <!-- THYMELEAF: Template Engine (Spring3-specific version) -->
    <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
        <property name="templateResolvers">
            <set>
                <ref bean="emailTemplateResolver" />
                <ref bean="webTemplateResolver" />
            </set>
        </property>
    </bean>

    <!-- THYMELEAF: View Resolver - implementation of Spring's ViewResolver interface -->
    <!-- (we would not need this if our app was not web)                              -->
    <bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

    <bean name="aopWakiliPersistence" class="wakiliproject.aspects.AopWakiliPersistence" />

    <context:annotation-config />
    <context:component-scan base-package="wakiliproject"/>

</beans>
。。。 发送

。。。 服务

。。。。。 控制器

@Controller
public class ThymeMainController {

    @Autowired
    private ThymeEmailService thymeEmailService;

    @RequestMapping(value = "/sendMailWithInlineImage", method = RequestMethod.POST)
    public void sendMailWithInline(
            @RequestParam("recipientName") final String recipientName,
            @RequestParam("recipientEmail") final String recipientEmail,
            @RequestParam("image") final MultipartFile image,
            final Locale locale)
            throws MessagingException, IOException {

        this.thymeEmailService.sendMailWithInline(
                recipientName,
                recipientEmail,
                image.getName(),
                image.getBytes(),
                image.getContentType(),
                locale);

    }

    public void send() throws MessagingException, IOException {

        MockMultipartFile mockMultipartFile = new MockMultipartFile(
       "test.txt",                //filename
       "Hallo World".getBytes());

        Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();

        sendMailWithInline("Oliver Muchai Githire", "tryrevilo@yahoo.com", mockMultipartFile, aLocale);
    }

}

请完全删除bean TemplateEngineCostom,因为您已经在bean templateEngine中正确定义了emailTemplateResolver。模板模式也可以是HTML5(),感谢您的回复,Patrick LC。我试过了,但没有任何改变。也许你需要为你的电子邮件模板定义另一个资源位置,检查你是否可以下载并在中运行该示例
@Controller
public class SpringThymeSend {

    @Autowired
    ThymeMainController thymeMainController;

    public void springThymeSend() throws MessagingException, IOException {
        thymeMainController.send();
    }

}
@Service
public class ThymeEmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    public void sendMailWithInline(
            final String recipientName,
            final String recipientEmail,
            final String imageResourceName,
            final byte[] imageBytes,
            final String imageContentType,
            final Locale locale)
            throws MessagingException {

        // Prepare the evaluation context
        final Context ctx = new Context(locale);
        ctx.setVariable("name", recipientName);
        ctx.setVariable("subscriptionDate", new Date());
        ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
        ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML

        // Prepare message using a Spring helper
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper message
                = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
        message.setSubject("Example HTML email with inline image");
        message.setFrom("tryrevilo@yahoo.com");
        message.setTo(recipientEmail);

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

        // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
        final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
        message.addInline(imageResourceName, imageSource, imageContentType);

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

}
@Controller
public class ThymeMainController {

    @Autowired
    private ThymeEmailService thymeEmailService;

    @RequestMapping(value = "/sendMailWithInlineImage", method = RequestMethod.POST)
    public void sendMailWithInline(
            @RequestParam("recipientName") final String recipientName,
            @RequestParam("recipientEmail") final String recipientEmail,
            @RequestParam("image") final MultipartFile image,
            final Locale locale)
            throws MessagingException, IOException {

        this.thymeEmailService.sendMailWithInline(
                recipientName,
                recipientEmail,
                image.getName(),
                image.getBytes(),
                image.getContentType(),
                locale);

    }

    public void send() throws MessagingException, IOException {

        MockMultipartFile mockMultipartFile = new MockMultipartFile(
       "test.txt",                //filename
       "Hallo World".getBytes());

        Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();

        sendMailWithInline("Oliver Muchai Githire", "tryrevilo@yahoo.com", mockMultipartFile, aLocale);
    }

}