JSP vs Thymeleaf Support in Spring Boot

In this article, we will discuss JSP vs Thymeleaf support in Spring Boot and which one is better to use with Spring boot.

I have used JSP and Thymeleaf with Spring boot to develop a Spring MVC web application. I am going to suggest to you which one has good support either JSP or Thymeleaf to work with Spring boot.


JSP vs Thymeleaf Support in Spring Boot

Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSPs. Also, many other templating engines include their own Spring MVC integrations.

Spring Boot includes auto-configuration support for the following templating engines:

For example, when you add thymeleaf engine starter dependency to spring boot project in pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Spring boot will automatically configure all the beans for thymeleaf to integrate with Spring boot project such as ViewResolver etc.

Read this Spring boot Thymeleaf tutorial at Spring Boot Thymeleaf Example Tutorial.

When you use one of these templating engines with the default configuration, your templates are picked up automatically from src/main/resources/templates.

Now, let's discuss about JSP support in the Spring boot project.

From Spring boot official documentation:

If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.
Spring boot official documentation says that JSP's should be avoided because it has some known limitations with embedded servlet containers (Tomcat, jetty, undertow etc).

Let's see what are know JSP limitations.

JSP Limitations

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

  • With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
  • Undertow does not support JSPs.
  • Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
It clear that JSP has known limitations only with embedded servlet containers but not with integration so you can also use JSP with spring boot if you make a jar or war type packaging and deploy it in supporting web containers ( Jetty and Tomcat).

Check out this step by step Spring boot integration with JSP tutorial at https://www.javaguides.net/2020/05/spring-boot-jsp-example-tutorial.html

Spring boot team recommended to use the below template engines to work with Spring boot apps:
I have used Thymeleaf a lot so I highly you guys to use Thymeleaf (very simple plain HTML with custom tags) with Spring boot to develop a Spring MVC web application.

Conclusion

It clear that JSP has known limitations only with embedded servlet containers but not with integration. You can also use JSP with spring boot but make a jar or war type packaging and deploy it in supporting web containers ( Tomcat or Jetty).

I have used Thymeleaf a lot so I highly suggest you guys to use Thymeleaf (which very simple like plain HTML with custom tags) with Spring boot to develop a Spring MVC web application.

References

Comments