Issue with org.thymeleaf.context.Context and mail template resolver

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue with org.thymeleaf.context.Context and mail template resolver

balteo
Hello,

I am in reference to the following tutorial: http://www.thymeleaf.org/springmail.html 

I get this error:

ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][0] Exception processing template "/mail/passwordResetInfo.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]

Here is my Thymeleaf configuration:

<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
        <property name="prefix" value="/META-INF/mail/" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="order" value="1" />
    </bean>

    <bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="order" value="2" />
    </bean>

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

    <bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>

And my java code:
private void mailPasswordResetInfo(Member member) throws MessagingException {
        final Context ctx = new Context();
        ctx.setVariable("member", member);
        final MimeMessage mimeMessage = mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
        message.setSubject("Password Reset Info");//TODO
        message.setFrom("thymeleaf@example.com");//TODO
        message.setTo(member.getEmail());
        final String htmlContent = templateEngine.process("passwordResetInfo.html", ctx);
        message.setText(htmlContent, true /* isHtml */);
        mailSender.send(mimeMessage);
    }

Can someone please advise?

Regards,

J.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with org.thymeleaf.context.Context and mail template resolver

balteo
I changed the prefix property from
<property name="prefix" value="/META-INF/mail/" />
to
  <property name="prefix" value="META-INF/mail/" />
(notice I have removed the forward slash) and it now works.
I've also added a suffix property to the ServletContextTemplateResolver resolver.
Regards,
Julien.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with org.thymeleaf.context.Context and mail template resolver

Mohan
Thanks for the information.