Creating views with Thymeleaf

As previously defined, a view is a representation of data that exists in and is generated by an application. Views are the primary points of interaction that a user has with an application built with the MVC pattern. The view layer may utilize different technologies to render information to a user. Spring supports a number of view options. These view options are also referred to as templates. Template support in a Spring application is provided by a template engine. Simply put, a template engine enables the utilization of static template files with the view layer of an application. A template engine may also be referred to as a template library. The following template libraries are available for use with Spring:

  • Thymeleaf
  • JSP/JSTL
  • Freemaker
  • Tiles
  • Velocity

This list is by no means intended to be exhaustive. There are a number of other template libraries available for use with Spring. We shall be relying on Thymeleaf to provide template processing support for our application. If you recall, we included templating support with Thymeleaf upon initial creation of our project. Inspecting the dependencies section of our project's pom.xml file will reveal the addition of Thymeleaf to our project:

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

At this juncture, you may desire a more formal definition of what exactly Thymeleaf is. As stated on Thymeleaf's official website, Thymeleaf is a modern server-side Java template engine for both web and standalone environments. Thymeleaf's main goal is to bring elegant natural templates to your development workflow - HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams. If you require a deeper understanding of Thymeleaf and its goals, you can find more information about it at http://thymeleaf.org.

In the previous chapter, we took a simple example of view creation in Spring by implementing a hello.html view for the HelloController. However, the view we created only displayed a Hello world! message to its viewers. We will be creating slightly more complex views in this chapter. We shall start by creating a view facilitating user registration on the platform.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.65