Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of using Thymeleaf instead of JSP in Spring?

I'm wondering what are the advantages I can get by using Thymeleaf instead of JSP for the view in Spring.

like image 437
Ragnar921 Avatar asked Aug 29 '17 06:08

Ragnar921


People also ask

Is Thymeleaf better than JSP?

Thymeleaf is way better in my opinion because it have good underlying priciples and exploits natural behaviour of browsers. Jsp makes html hard to read, it becomes weird mixture of html and java code which makes a lot of problems in comunication between designer - developer.

What is the advantage of using Thymeleaf?

It allows for natural templating, can do complex processing and lets us easily define custom dialects. On top of that, Thymeleaf facilitates the collaboration of both front end and back end developers on the same template file, greatly increasing productivity.

Is Thymeleaf faster than JSP?

From what I read, Thymeleaf is pretty slow compared to other templating languages, while JSP is very fast, with FreeMarker and Velocity coming close. However, Thymeleaf supports natural templates (templates that will render nicely in your browser even if you don't run them through the template engine).

Can we use Thymeleaf with JSP in Spring Boot?

If you are building web front-ends with Spring Boot or Spring MVC, and you're still using JSP (Java Server Pages) then this course is for you. Thymeleaf is a great templating engine which replaces JSP, and you can easily use it in any Spring MVC or Spring Boot application.


1 Answers

The commonly cited advantages are:

  • Spring integration is a first class aspect of Thymeleaf (plenty of documentation here). "First class", in this context, means that it is not accidental or partially implemented, it is a deliberate, well supported aspect of Thymeleaf.
  • The Spring Expression Language is more powerful than JSP Expression Language. "More powerful" sounds subjective but in this case we are talking about integration with Spring so Spring's own EL (with its awareness of model attributes, form backing bean and internationalization) offers more out-of-the-box than vanilla JSP expressions.
  • Thymeleaf provides useful formatting utilities such as ${#calendars.format(...)}, ${#strings.capitalize(...)} which are well integrated with Spring e.g. you can pass model beans propagated by Spring MVC into these functions.
  • The build/deploy/test feedback loop is shortened by Thymeleaf. Here's an example; let's say you want to change the layout or style for a webpage. In Thymeleaf this involves: (1) open the .html template, edit it and (possibly) edit it's linked .css file; (2) hit F5 to refresh in the browser; (3) repeat until happy. By contrast, the same activity in a JSP implementation would involve: (1) deploy the application into a development server; (2) start it up; (3) make some changes; (4) redeploy (or hot deploy) the changes; (5) repeat until happy.
  • The last point hints strongly at this limitation of JSPs; they cannot be used outside of a container. By contrast, Thymeleaf templates can be used outside of a container. For example; Spring's MVC test support is well integrated with Thymeleaf and can allow you to render (and test/assert against) a resolved Thymeleaf template within a test context without starting your application.
  • Thymeleaf templates look like HTML (the term is "natural template"), they can even be rendered in the browser as static content (with template/prototype data) so if your web layer and backend are authored by different people/specialisations then Thymeleaf is easier for a web designer to deal with than JSP.
like image 66
glytching Avatar answered Sep 28 '22 16:09

glytching