본문 바로가기

[Thymeleaf] 왜 JSP대신 Thymeleaf 를 써야할까

I'm 영서 2022. 4. 20.
반응형

프로젝트에 Thymeleaf를 사용햇는데 Thymeleaf와 JSP의 차이점에 대해서 질문을 받았다.

대답할 수 없었다.

개인적으로는 특정 html tag에 기능을 추가하는것 이라고 느꼇기 때문에 기능상으로는 다른게 없다고 답하고 다른 차이는 모르겠다고 답햇는데, 

나도 정확한 차이를 모르고 쓴다는게 너무 부끄러운 일이라 바로 검색해서 찾아봤다.

아무튼 두개의 차이는 html tag에 바로 입력이 가능하다라는 차이라고 말씀을 들을 수 있었다.

그래서 그 알려주신 사항을 기조로 하여 찾아봤다.

 

참조문서링크는 더보기

 

 

우선 Thymeleaf가 무엇인지 부터 확인해보자

What is Thymeleaf?

The Thymeleaf is an open-source Java library that is licensed under the Apache License 2.0.

It is a HTML5/XHTML/XML template engine. It is a server-side Java template engine for both web (servlet-based) and non-web (offline) environments. It is perfect for modern-day HTML5 JVM web development. It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates. It is based on XML tags and attributes. These XML tags define the execution of predefined logic on the DOM (Document Object Model) instead of explicitly writing that logic as code inside the template. It is a substitute for JSP.

The architecture of Thymeleaf allows the fast processing of templates that depends on the caching of parsed files. It uses the least possible amount of I/O operations during execution.

 

JavaTPoint에서 보면

It is appropriate for serving XHTML/HTML5 in web applications , HTML5를 제공해는데 적합하며

It is a substitute for JSP. JSP를 대체하고

It uses the least possible amount of I/O operations during execution

빠르게 처리하며, 실행중 가능한 최소한 I/O 작업을 한다고 한다.

 

그렇다면 왜 JSP대신 Thymeleaf를 써야할까?

JSP is more or less similar to HTML. But it is not completely compatible with HTML like Thymeleaf. We can open and display a Thymeleaf template file normally in the browser while the JSP file does not.Thymeleaf supports variable expressions (${...}) like Spring EL and executes on model attributes, asterisk expressions (*{...}) execute on the form backing bean, hash expressions (#{...}) are for internationalization, and link expressions (@{...}) rewrite URLs.

Like JSP, Thymeleaf works well for Rich HTML emails.

JSP is more or less similar to HTML. But it is not completely compatible with HTML

완벽히 HTML과 호환되기 때문이라고 생각할 수 있겠다!

 

즉 따로 JSP파일을 생성해서 그 JSP파일 내에서 Elements~~ 한 작업들에 대해 HTML에서 바로 작업 할 수 있게 되는점으로 인해 Thymeleaf를 사용한다고 볼수 있겠다..!

 

Spring의 공식문서는 JSP와 Thymeleaf 사용시 차이점을 가지고 비교하는데

Email 과 Type을 가지고 있는 한 페이지를 기준으로

버튼 이미지를 변경할때 JSP와 Thymeleaf를 사용하는 예제를 보여준다.

이때 예제를 읽어보면 곧바로 html에 다이렉트로 옵션을 넣을 수 있는걸 볼 수 있다.

 

html tag에 바로 입력이 가능하다 라는 차이를 확인할 수 있었다. 

반응형

댓글