Open In App

New Features of Spring MVC 6.0 and Thymeleaf 3.0

Last Updated : 17 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the latest features of Spring MVC 6.0 and Thymeleaf 3.0 and how they can be leveraged to develop enterprise applications.

Spring MVC is a fundamental and broader part of the Spring Framework. It is used to develop Java-based applications. Spring MVC uses a front controller pattern – all requests go through the DispatcherServlet which handles request routing. Annotations like @Controller, @RequestMapping are commonly used to build controller classes and map URLs to handler methods. Spring MVC makes it easy to build RESTful web services and integrate them with other Spring projects like Spring Security, Spring Data JPA, etc.

Thymeleaf 3.0 was a major release that brought many improvements and new capabilities. One of the biggest changes is support for HTML5 parsing, in the Previous versions of Thymeleaf parsed templates as XML, which could be limiting for HTML5 features like optional closing tags. Thymeleaf 3.0 fully supports HTML5 parsing out of the box.

Spring MVC 6.0 – New Features

  • Reactive Support: Spring MVC 6.0 has first-class support for reactive programming with Reactor and RxJava. Controllers can now return Mono/Flux types to support asynchronous and non-blocking requests.
  • Handler Method Return Types: Handler methods now support returning CompletableFuture, DeferredResult, and AsyncResult in addition to traditional ModelAndView and ResponseEntity.
  • Custom Handler Method Arguments: Method parameters can be annotated to bind to specific parts of the request like path variables, request parameters, etc. rather than relying on request mapping attributes.
  • Module Structure: Spring MVC code is split into logical modules for easier consumption of specific features.
  • Async Support: Asynchronous request processing with Servlet 4.0 async support and non-blocking I/O.
  • WebFlux Integration: Tight integration with Spring WebFlux for building reactive web applications.

For Example, here we have developed a Student Management System,

There are simple steps to interact with a simple web application built using Spring MVC:

  • Request the application’s home page URL (e.g. localhost:8080). This will typically be handled by a controller method mapped to the root URL that shows all student details like student name, age, and enrolled course.
  • The controller method will return a view name (e.g. “student”). Spring MVC will resolve this view name to a physical view (JSP, HTML, etc.) and render it.
  • The view will be populated with any model attributes added to the Model by the controller. These can be used in the view template (e.g. ${name}).
  • The view will be returned as the HTTP response to the student’s home page request.
  • To submit a form or request data, provide form parameters of the student that we are mentioned in the student Entity class in the request and POST it to a specific URL mapped to a controller handler method.
  • The handler method retrieves the parameters from the request, performs operations, and returns a view name to render.
  • To view the response of a student, GET the same or different URL. The controller returns the view populated with any updated model data.
  • Links and buttons in the UI can trigger GET requests to other controller methods to display related pages/data.
  • Errors are typically handled by adding a BindingResult parameter to catch validation errors. Custom errors show on the view.
  • Use the browser’s network tools and debug logs to trace requests, parameters, and responses for debugging.

Dependency:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.1</version>
</dependency>

Thymeleaf 3.0 Features

  • HTML5 Support: Thymeleaf 3.0 fully supports HTML5 parsing out of the box. It can parse templates as HTML5 rather than XML.
  • Fragment Caching: Fragments (reusable template code blocks) can now be cached to improve performance. The cache is automatically invalidated when fragments change.
  • Enhanced Internationalization (i18n): New i18n dialect with features like pluralization, locale-specific formatting of numbers/dates etc. Easier internationalization of templates.
  • Layout Dialect: Defines template layouts that can be extended by other templates for DRY template design.
  • Link Rewriting: Automatic rewriting of relative links/references when using fragments.
  • Asynchronous Support: Async processing support with features like deferred expressions.
  • Modularization: Thymeleaf code is split into subprojects for easier consumption of specific features.
  • Java 8 Support: Full support for Java 8 features like lambdas, streams, date/time API, etc.

Required configuration for Thymeleaf

  • Add Thymeleaf dependency in pom.xml
  • Configure ThymeleafTemplateResolver bean in the servlet context file: URLs. We set prefix, suffix, and cacheable.
  • Configure TemplateEngine bean: We set templateResolver, additional properties etc.
  • Configure ViewResolver bean: This resolves view names to views.
  • Annotate controller methods to return “thymeleaf view name” strings.
  • Create template files (html files) in the specified prefix location with .html suffix.
  • Add model attributes in controller and access in templates using ${…} syntax.

Dependency:

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>

In Spring MVC, HTTP templates are reusable patterns for common HTTP requests like GET, POST, PUT, DELETE, etc. Some of the main HTTP request templates are:

  • GET: Used to request representation of a resource. No body, parameters are passed via URL or headers.
  • POST: Used to create a new resource and submit data to be processed.
  • PUT: Used to update an existing resource fully or partially.
  • DELETE: Used to delete a resource.

Conclusion

In conclusion, Spring MVC 6.0 and Thymeleaf 3.0 have new features that enhance developers’ experience with more useful, minimalistic configurations and more functional approaches


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads