Thymeleaf : http://www.thymeleaf.org
Thymeleaf Spring mail 샘플 : http://www.thymeleaf.org/springmail.html
Thymeleaf 태그 설명 : http://www.thymeleaf.org/standarddialect5minutes.html
git : https://github.com/thymeleaf
Thymeleaf Tiles 결합 : https://github.com/thymeleaf/thymeleaf-extras-tiles2


[참고]
james 메일 보내기 : http://tyboss.tistory.com/entry/Java-James-서버-사용하여-메일mail-보내기


Inputstream 사용 : https://stackoverflow.com/questions/53323313/java-thymeleaf-how-to-process-an-inputstream-in-templateengine-stand-alon


1. Maven으로 Thymeleaf Library 추가하기

 <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>2.0.16</version>
 </dependency>
 <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring3</artifactId>
      <version>2.0.16</version>
 </dependency>




2. Spring에 Thymeleaf 설정하기

 <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
   <!--
    ClassLoaderTemplateResolver의 기본 경로는 소스 경로 즉 /WEB-INF/classes 부터 시작이다.
    -->
   <property name="prefix" value="templates/" />
   <property name="suffix" value=".html" />
   <property name="templateMode" value="HTML5" />
   <property name="characterEncoding" value="UTF-8" />
   <!-- <property name="cacheable" value="false" /> -->
   <property name="order" value="1" />
  </bean>
  
   <!--
   ServletContextTemplateResolver는 sitemesh나 tiles와 경로 설정은 똑같이 하면 된다.
   Jrebel 적용시 ClassLoaderTemplateResolver는 cacheable를 false로 해도 적용이 되지 않기 때문에 ServletTemplateResolver로 테스트 후 바꿔도 된다.

   -->
   <bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
   <property name="prefix" value="/WEB-INF/presentation/jsp/web/templates/" />
   <property name="suffix" value=".html" />
   <property name="templateMode" value="HTML5" />
   <property name="characterEncoding" value="UTF-8" />
   <!-- <property name="cacheable" value="false" /> -->
   <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>
    <!-- Resolver를 한개만 사용할 경우
    <property name="templateResolver" ref="emailTemplateResolver" />
    -->
  </bean>



3. Template 만들기
    다음에서 설명하겠지만 Context.setVariable(key, value) 로 변수값을 설정 후에 아래 빨간색 부분처럼 사용할 수 있다. 일반 텍스트는 th:text="${key}" 로 사용하고 URL은 th:href="@{${key}}" 로 사용한다.

- template.html

 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title th:remove="all">Template for HTML email with inline image</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <span th:text="${name}"></span>
        <p th:text="#{greeting(${name})}">
            Hello, Peter Static!
        </p>
        <p th:if="${name.length() > 10}">
            Wow! You've got a long name (more than 10 chars)!
        </p>
        <p>
            You have been successfully subscribed to the <b>Fake newsletter</b> on
            <span th:text="${#dates.format(subscriptionDate)}">28-12-2012</span>
        </p>
        <p>Your hobbies are:</p>
        <ul th:remove="all-but-first">
            <li th:each="hobby : ${hobbies}" th:text="${hobby}">Reading</li>
            <li>Writing</li>
            <li>Bowling</li>
        </ul>
        <p>
            You can find <b>your inlined image</b> just below this text.
        </p>
        <p>
            <img src="sample.png" th:src="'cid:' + ${imageResourceName}" />
        </p>
        <p>
                 <a th:href="@{${homepageUrl}}" style="text-decoration:none;">내 홈페이지</a><br />
                 <a th:href="@{'http://' + ${naverUrl}}" style="text-decoration:none;">네이버</a>
        </p>
        <p>
            Regards, <br />
            &emsp; <em>The Thymeleaf Team</em>
        </p>
    </body>
</html>



4. Controller에서 Template 불러오기

    4-1. ClassLoaderTemplateResolver 사용시

@RequestMapping("mail.do")
 public void mail(@RequestParam Map<String, Object> paramMap,
   @Value("#{global['server.host']}") String serverHost,
   HttpServletRequest request,
   final Locale locale,
   ModelMap model) throws Exception {
  log.debug("***************************************************************************************");
  log.debug("*** paramMap : " + paramMap);
  log.debug("***************************************************************************************");
  
  final Context ctx = new Context(locale);

// 변수 설정
ctx.setVariable("name", "홍길동");

/*
  Map<String, String> mailMap = new HashMap<String, String>();
  String usrKey = RandomStringUtils.random(20, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  mailMap.put("usrKey", usrKey);
  mailMap.put("serverUrl", "http://" + serverHost + request.getContextPath());
  mailMap.put("usrNm", "홍길동");
  mailMap.put("loginId", "honggd");

  if(paramMap != null) {
   Iterator<String> iter = paramMap.keySet().iterator();
   String key = null;
   while(iter.hasNext()) {
    key = iter.next();
    ctx.setVariable(key, paramMap.get(key));
   }
  }
*/

  String template = templateEngine.process("template", ctx);  
  log.debug(template);  // html template가 변수로 치환되어 String으로 반환한다. 이 String을 메일 보낼 때 내용으로 사용하면 된다.
 }


    4-2. ServletContextTemplateResolver 사용시

@Autowired
private ServletContext servletContext;

@RequestMapping(value="/send.do", method=RequestMethod.POST)
 public void send(@RequestParam Map<String, Object> paramMap,
   @Value("#{global['server.host']}") String serverHost,
   @Value("#{global['mail.admin.email']}") String mailAdminEmail,
   @Value("#{global['mail.admin.name']}") String mailAdminName,
   HttpServletRequest request,
   HttpServletResponse response,
   final Locale locale,
   ModelMap model) throws Exception{
  log.debug("***************************************************************************************");
  log.debug("*** paramMap : " + paramMap);
  log.debug("***************************************************************************************");
  
  model.addAttribute("paramMap", paramMap);
  
  // ClassLoaderTemplateResolver 를 사용할 때와 Context를 생성하는 부분만 다르다.
  final WebContext ctx = new WebContext(request, response, servletContext, locale);
 
  ctx.setVariable("usrNm", "홍길동");  

  String template = templateEngine.process("template", ctx); 
  log.debug(template);
}


  4-3. StringTemplateResolver 사용시

StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
IContext context = new Context(SpringUtil.getLocale(), dataBox);
String out = templateEngine.process(html, context);


 

+ Recent posts