简体   繁体   中英

What template engine should I use in Spring MVC?

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?

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.

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.

There are many template engines available out there. But Spring Boot officially supports Thymeleaf, FreeMarker, Mustache and Groovy Templates. My preference is Thymeleaf because of its extendability.

There is a detailed comparison on various aspects of template engines explained in the below post.

https://springhow.com/spring-boot-template-engines-comparison/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM