iTranslated by AI
Trying out Java + Spring Boot + Thymeleaf with docker-compose
I said next time I would try connecting to a database? That was a lie.
No, I just thought I should touch on the template engine first... 💦
TL;DR
- This is about the article below + the introduction of Thymeleaf
- Trying to create a Java + Spring Boot environment under docker-compose | Junya Kitayama | zenn
- I am also referring to some parts of the following article
- A run-up book for getting started with SpringBoot (updated as needed) | Qiita
What is Thymeleaf?
Thymeleaf is a template engine recommended for use with Spring Boot.
It's like ERB in Ruby on Rails or Razor Pages in ASP.NET Core.
If you are going to use Spring Boot not just as a Web API server but also to create views using a template engine, you will likely use it.
- Thymeleaf
- Thymeleaf Standard Expression Syntax | Java Suki
- Memo on how to use Thymeleaf in Spring Boot | Qiita
Prepare docker-compose.yml
cd your_project
mkdir server
touch docker-compose.yml
docker-compose.yml
version: '3.6'
services:
app:
image: openjdk:15
ports:
- 8080:8080
tty: true
volumes:
- ./server:/srv:cached
working_dir: /srv
- OpenJDK | DockerHub
- JDK Project Releases | OpenJDK
- Version information that can be specified is here
- http://openjdk.java.net/projects/jdk/
Create a Gradle Project
This time, I will also create it using a site called Spring Initializr.
- Spring Initializr
The difference from the previous article is the part outlined in red.
If you are continuing from the previous article, you can just add
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
to server/build.gradle.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
...
After finishing the input, click GENERATE and download the file.
Once downloaded, place the extracted content under your_project/server.
After placing it, server/src/main/java/com/example/api/ApiApplication.java is fine as is for now.
Let's briefly try the template engine Thymeleaf for now
Create a file named server/src/main/resources/templates/index.html and edit it as follows:
touch server/src/main/resources/templates/index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'Is this Thymeleaf?'">html-heading 1</h1>
<p>Yes, it is recommended by SpringBoot.</p>
</body>
</html>
Start the Docker Container, Build with Gradle, and Start the Application
docker-compose up -d
docker-compose exec app bash
bash-4.4# pwd
/srv
bash-4.4# sh gradlew build
...
BUILD SUCCESSFUL in 6m 37s
6 actionable tasks: 6 executed
bash-4.4# java -jar build/libs/api-0.0.1-SNAPSHOT.jar
Once it's running, access http://localhost:8080/ to check it.
It was displayed even though no routing was configured.
It seems Thymeleaf handles this kind of routing automatically.
(As a test, I prepared index.html without Thymeleaf, and it resulted in a routing error.)
Now, let's stop the application once with Ctrl+c.
Trying to Display a View via a Controller
mkdir server/src/main/java/com/example/api/controllers
touch server/src/main/java/com/example/api/controllers/IndexController.java
package com.example.api.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping({ "/", "/index" })
public ModelAndView get(ModelAndView mav) {
mav.addObject("target", "Thymeleaf");
mav.setViewName("index");
return mav;
}
}
Let's also edit the server/src/main/resources/templates/index.html file slightly.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'Is this Thymeleaf?'">html-heading 1</h1>
<p>Yes, it is recommended by SpringBoot.</p>
<!-- Add the following line -->
<div th:text="'Hello, ' + ${target} + '!!'"></div>
</body>
</html>
After compiling, let's display it again.
bash-4.4# sh gradlew build
bash-4.4# java -jar build/libs/api-0.0.1-SNAPSHOT.jar
This time, the value set in the ModelAndView on the Controller side is displayed!
Great job.
References
- A run-up book for getting started with SpringBoot (updated as needed) | Qiita
- Trying Spring Boot Part 2 - Controller, Template Engine (Thymeleaf) - | endok's blog
Here is the repository for this project:
Discussion