What template engine should I use in Spring MVC?

13,350

Solution 1

The best practices for server-side rendering have shifted towards using a template engine. These get the responsibility to merge the data with the template into the actual output.

Current preferences appear to be:

JSP's and JSF are entirely different things and have become out of fashion.

The big plus for using an actual template engine is that you are forced to separate the concerns of gathering the data to present and rendering it; this separation allows you to (unit) test the templates.

Note, however, that the industry is shifting once more towards client-side rendering, where the server just returns the data as JSON-objects and the web application uses some framework like Angular, React, jQuery or Ember to build the pages.

Note on the edit: Originally the list included Velocity, but that is no longer supported by Spring.

Solution 2

You can use any of them as they are supported. JSP, FreeMaker and Thymeleaf are similar by idea: you create a template to be rendered. JSP and FreeMaker lacks some features that are available in Thymeleaf.

I like Thymeleaf's idea where you can load your template to the browser and see how the page is going to be rendered (real). Thymeleaf template is fully featured HTML page. That's not possible in JSP where you have JSP tags and FreeMaker where you have placeholders.

JSF is component based so that's a different approach.

If I have to choose I would use Thymeleaf.

Share:
13,350
Sergey Bakotin
Author by

Sergey Bakotin

Updated on July 04, 2022

Comments

  • Sergey Bakotin
    Sergey Bakotin almost 2 years

    I'm learning Spring MVC and want to create a site. The main problem is a template system. Should I use JSP / JSF / Apache FreeMarker / Thymeleaf or something else?

    I saw a lot of discussion on this subject, but they are all outdated. So, I'm curious, what is fine now?

  • user2501323
    user2501323 about 5 years
    Note, that Velocity support is removed from Spring since 5.0.1 : jira.spring.io/browse/SPR-13795