Ragnar921
Ragnar921

Reputation: 977

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.

Upvotes: 14

Views: 10551

Answers (1)

glytching
glytching

Reputation: 47935

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.

Upvotes: 14

Related Questions