Thymeleaf is a templating engine that gets particular attention from the Spring community.
Its success is due mostly to its friendly syntax (it almost looks like HTML) and the ease with which it can be extended.
Various extensions are available and integrated with Spring Boot:
Support |
Dependency |
---|---|
Layouts |
|
HTML5 data-* attributes |
|
Internet Explorer conditional comments |
|
Support for spring security |
|
A very good tutorial on Thymeleaf's integration with Spring can be found at http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html.
Without further ado, let's add the spring-boot-starter-thymeleaf
dependency to bootstrap the thymeleaf templating engine:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'masterSpringMvc'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
We will now add the first page to our application. It will be located in src/main/resources/templates
. Let's call the file resultPage.html
:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>Hello thymeleaf</title> </head> <body> <span th:text="|Hello thymeleaf|">Hello html</span> </body> </html>
We can see from the very start that Thymeleaf integrates perfectly with html and its syntax almost feels natural.
The th:text
value is put between pipes. It means that all the values inside the text will be concatenated.
It might seem a bit awkward at first, but in practice, text will rarely be hardcoded in our pages; so, Thymeleaf makes an opinionated design decision here.
Thymeleaf has a big advantage for web designers: everything that is dynamic inside the templates can fall back to a default value in the case where they are opened without the server running. Resource URLs can be specified relatively and every markup can contain placeholders. In our previous example, the text "Hello html" would not be displayed when the view is rendered in the context of our application, but it will if the file is opened directly with a web browser.
To speed up development, add this property to your application.properties
file:
spring.thymeleaf.cache=false
This will disable the view cache and cause templates to reload every time they are accessed.
Of course, this setting will need to be disabled when we go into production. We will see that in Chapter 7, Optimizing Your Requests.
Lastly, we will need to modify our HelloController
class. Instead of displaying plain text, it must now route to our freshly created view. To accomplish this, we will remove the @ResponseBody
annotation. Doing so and still returning a string will tell Spring MVC to map this string to a view name instead of displaying a particular model directly in the response.
Here is what our controller now looks like:
@Controller public class HelloController { @RequestMapping("/") public String hello() { return "resultPage"; } }
In this example, the controller will redirect the user to the view name resultPage
. The ViewResolver
interface will then associate this name with our page.
Let's launch our application again and go to http://localhost:8080
.
You will see the following page: