<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

    <title th:text="${title}">Report mail</title>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>

    <h1>Send Test Report Email</h1>

    <table>

        <thead>

        </thead>

        <tbody>

        <colcolgroup>

        <col style="width: 100px;"/>

        <col/>

        </colcolgroup>

            <tr>

                <td th:text="#{mail.th.project.id}">Proejct</td>

                <td th:text="${projectId}">Project</td>

            </tr>

            <tr>

                <td th:text="#{mail.th.sub.project.id}">Sub Project</td>

                <td th:text="${subProjectId}">Sub Project</td>

            </tr>

            <tr>

                <td th:text="#{mail.th.duration}">Duration</td>

                <td th:text="${duration}">Duration</td>

            </tr>

            <tr>

                <td th:text="#{mail.th.format}">Report Format</td>

                <td th:text="${format}">Document</td>

            </tr>

            <tr>

                <td th:text="#{mail.th.start.datetime}">Start Datetime</td>

                <td th:text="${startDateTime}"></td>

            </tr>

            <tr>

                <th th:text="#{mail.th.end.datetime}">End Datetime</th>

                <td th:text="${endDateTime}"></td>

            </tr>

        </tbody>

    </table>

</body>

</html> 

위의 템플릿 코드는 그대로 둔 채,

<!-- Thymeleaf template -->

<dependency>

    <groupId>org.thymeleaf</groupId>

    <artifactId>thymeleaf</artifactId>

    <version>2.0.17</version>

</dependency>

<dependency>

    <groupId>org.thymeleaf</groupId>

    <artifactId>thymeleaf-spring3</artifactId>

    <version>2.0.17</version>

</dependency>

아래의 오류가 발생한다. 

ERROR: org.thymeleaf.TemplateEngine - [THYMELEAF][http-bio-8080-exec-7] Exception processing template "report/testReportMail": Exception evaluating SpringEL expression: "title" (report/testReportMail:4)

thymeleaf 의 version을 2.0.17 에서 2.0.16으로 변경해보자.

정상동작한다. 

현재 사용하고 있는 스프링은 '3.2.0.RELEASE'


이메일 발송용 템플릿 엔진으로 사용하려고 Thymeleaf 를 찾았다.

전 프로젝트에서는 StringTemplate3를 사용했었는데, StringTemplate4로 바뀌면서 그 사용법이 많이 바뀐 탓에 새로 익혀야할 것 같아 찾다보니 몇몇 사람들의 추천하는 글을 보고는 덜컥 시도를 해본다.

ㅡ_-);; 주된 삽질의 끝은 오탈자였다. 하아...

Spring에서 사용하는 예제 : http://www.thymeleaf.org/springmail.html

<!-- 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/templates/" /> 

  <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> 



내가 적용한 코드

<!-- THYMELEAF: Template Resolver for email templates -->

    <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">

        <property name="prefix" value="META-INF/template/mail" />

        <property name="suffix" value=".html"/>

        <property name="templateMode" value="HTML5" />

        <property name="characterEncoding" value="UTF-8" />

        <property name="order" value="1" />

        <!-- Template cache is true by default. Set to false if you want -->

        <!-- templates to be automatically updated when modified.        -->

        <property name="cacheable" value="true" />

    </bean>

    

    <!-- THYMELEAF: Template Engine (Spring3-specific version) -->

    <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">

        <property name="templateResolvers">

          <set>

            <ref bean="emailTemplateResolver" />

          </set>

        </property>

    </bean>

이렇게 설정을 해놓으니 계속 

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "email-test", template might not exist or might not be accessible by any of the configured Template Resolvers

TemplateEngine에서 템플릿 파일을 찾지 못한다는 오류를 뿌린다. ㅡ_-);;

2시간 정도 삽질을 한 결과를 알아냈다.

<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">

        <property name="prefix" value="META-INF/template/mail" />

...

</bean>

의 항목을

<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">

        <property name="prefix" value="META-INF/template/mail/" />

...

</bean>

으로 바꾸니까 된다.

미묘한 차이인데, 발견했는가? ㅡ_-)?

예제의 '/' 하나를 무시한 결과가 나의 3시간 삽질의 차이를 낳았다. Orz.

+ Recent posts