DEV Community

HEEAH IM
HEEAH IM

Posted on

Thymeleaf

What is Thymeleaf

Thymeleaf는 server-side JAVA 기반의 template engine 이다.
Spring boot 가 권장하는 템플릿 엔진 중 하나이다.(Groovy, Thymeleaf, FreeMarker..) 그동안 사용해왔던 jsp보다 더 간단한 설정과 표준 문법으로 HTML을 작성할 수 있다. Thymeleaf는 웹 표준, 특히 HTML5 에 맞춰 설계되었기에 완전히 검증된 템플릿을 만들 수 있다.
JSP의 경우, tag libraries를 사용하는 경우, HTML 표준 태그가 아니기에 브라우저가 해석할 수 없는 코드가 된다.

<form:inputText name="userName" value="${user.name}" />
Enter fullscreen mode Exit fullscreen mode

Thymeleaf를 이용하여 작성한 경우, 브라우저가 해석할 수 있는 코드가 된다. 문법 역시 깔끔하게 보인다.

<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
Enter fullscreen mode Exit fullscreen mode

이로 인해 퍼블리셔, 디자인과 개발팀의 격차를 해소하며 커뮤니케이션을 개선할 수 있는 것을 어필한다.(공식 doc에서..)

Standard Expression Syntax

[Simple expressions]
Variable Expressions: ${...}
Selection Variable Expressions: *{...}
Message Expressions: #{...}
Link URL Expressions: @{...}
Fragment Expressions: ~{...}

[Text operations]
String concatenation: +
Literal substitutions: |The name is ${name}|

Thymeleaf Doc

Tutorial: Using Thymeleaf

Conclusion

사용해보니 개발할 때도 Thymeleaf 가 훨씬 편하다. jsp의 경우 view 템플릿 레더링을 Servlet Container가 하기 때문에 결과를 보는데 딜레이가 있다. 반면 Thymeleaf의 경우는 Servlet Container 개입없이 최종 View를 완성하므로, 바로 확인이 수월하다. 테스트 시에도 Servlet Container를 띄울 필요가 없으므로 mockMVC으로도 테스트 코드 작성이 가능하다.
만약 frond-end 개발자가 Thymeleaf없이 view를 랜더링해서 만들고 있다는 경우라면, 이를 고려하여 attribute 도 추가해줘야 한다..

Top comments (0)