이메일 발송용 템플릿 엔진으로 사용하려고 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.