header

Spring Boot with Thymeleaf

Setup

This time I will show you how to create a basic project in Spring Boot with Thymeleaf, in order to do that you need to install SDKMAN if you are using Linux or Mac, or posh-gvm if you are using Windows. After that, you can easily install:

  • Spring Boot
  • Groovy
  • Gradle

Then execute this command in your terminal.

spring init --dependencies=web --language=groovy --build=gradle spring-boot-thymeleaf

This is the build.gradle file generated:

buildscript {
  ext {
    springBootVersion = '1.5.6.RELEASE'
  }
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  compile('org.springframework.boot:spring-boot-starter-web')
  compile('org.codehaus.groovy:groovy')
  testCompile('org.springframework.boot:spring-boot-starter-test')
}

Now, let’s create a RestController

package com.example.springbootthymeleaf.controller

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping

@RestController
class DemoController{

  @RequestMapping("/")
  String index(){
    'Hello World!'
  }

}

At this point you have an application Spring Boot running in 8080 port. Execute this command in order to see the Hello World message:

gradle bootRun

Hello World Concept

Next step is change from RestController to a Controller so we can render the Hello World message to a web page:

package com.example.springbootthymeleaf.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping

@Controller
class DemoController{

  @RequestMapping("/")
  String index(){
    'index'
  }

}

Next we need to add Thymeleaf dependency to the build.gradle file

buildscript {
  ext {
    springBootVersion = '1.5.6.RELEASE'
  }
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
  mavenCentral()
}


dependencies {
  compile('org.springframework.boot:spring-boot-starter-web')
  compile('org.codehaus.groovy:groovy')
  compile('org.springframework.boot:spring-boot-starter-thymeleaf')
  testCompile('org.springframework.boot:spring-boot-starter-test')
}

Finally we can create a simple web page in $PROJECT_HOME/src/main/resources/templates

<html>
  Hello World!
</html>

If you run the application again, you should see the index web page showing the Hello World message in localhost:8080

Sending model using Thymeleaf Template

Thymeleaf is a modern template engine for both web and standalone environments. Let’s create a simple model so we can send it to a web page

package com.example.springbootthymeleaf.model

class Person{
  String nickname
  String email
}

Another change we must do in the Controller is to return model and view:

package com.example.springbootthymeleaf.controller

import org.springframework.stereotype.Controller
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.bind.annotation.RequestMapping

import com.example.springbootthymeleaf.model.Person

@Controller
class DemoController{

  @RequestMapping("/")
  ModelAndView index(){
    Person person = new Person(nickname:'josdem',email:'joseluis.delacruz@gmail.com')
    ModelAndView modelAndView = new ModelAndView('index')
    modelAndView.addObject(person)
    modelAndView
  }

}

And you can see the model in the template using some Thymeleaf tags:

<html>
  <h3 th:text="${person.nickname}" />
  <p th:text="${person.email}" />
</html>

To Download the Project:

git clone https://github.com/josdem/spring-boot-thymeleaf.git

Return to the main article

comments powered by Disqus