Thymeleaf - 解析模板时出错" email-inlineimage.html"

时间:2014-07-05 03:40:34

标签: java spring-mvc thymeleaf

所以,我看到所有人都在Stackoverflow中回答了这个问题,但对我没有任何帮助。 ( SpringMVC+Thymeleaf ,error message is : template might not exist or might not be accessible by any of the configured Template Resolvers

Error resolving template "pages", template might not exist or might not be accessible by any of the configured Template Resolvers

SpringMVC+Thymeleaf ,error message is : template might not exist or might not be accessible by any of the configured Template Resolvers

... )

这是我的servlet-context.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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- Basic Configurations -->
<context:annotation-config/>

<context:component-scan base-package="com.podium.italia.controller"/>
<context:component-scan base-package="com.podium.italia.service"/>
<context:component-scan base-package="com.podium.italia.model"/>
<context:component-scan base-package="com.podium.italia.repository"/>

<mvc:annotation-driven />
<mvc:default-servlet-handler />

<!-- i18n -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/i18n"/>
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

<!-- Email support -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="protocol" value="smtp" />
    <property name="username" value="smmailsender@gmail.com" />
    <property name="password" value="Style@mix2014" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.quitwait">true</prop>
        </props>
    </property>
</bean>

<!-- THYMELEAF: Template Resolver for email templates -->
<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
    <property name="prefix" value="mail/" />
    <property name="templateMode" value="HTML5" />
    <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/" />
    <property name="templateMode" value="HTML5" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="order" value="2" />
</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>

<import resource="daoContext.xml"/>

这里是emailService:

final WebContext ctx = new WebContext(request,response, request.getServletContext(), locale);

//.....

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

错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "email-
inlineimage.html", template might not exist or might not be accessible by any of the
configured Template Resolvers
org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)
com.podium.italia.service.EmailService.sendMailWithInlineImages(EmailService.java:112)

1 个答案:

答案 0 :(得分:1)

我无法在您的servlet-context.xml中发现错误,因此问题必须隐藏在其他地方(您确定在mail/email-inlineimage.html上的类路径上有模板文件吗?)。我提供了整个项目的工作示例(因为将所有内容粘贴到此处的代码示例中是过分杀戮),您可以将其导入STS并运行。

Spring Thymeleaf Mailing