[Java] Thymeleaf Basic (Spring Boot)

What is Thymeleaf

how to use

dependencies {
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
}

Call template file

main.java


package sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

HelloController.java


package sample.thymeleaf.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("message", "Hello Thymeleaf!!");
        return "hello";
    }
}

hello.html


<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8" />
        <title>Hello Thymeleaf</title>
    </head>
    <body>
        <h1 th:text="${message}"></h1>
    </body>
</html>

Embed text in template file

HelloController.java


package sample.thymeleaf.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

hello.html


<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8" />
        <title>Hello Thymeleaf</title>
    </head>
    <body>
        <h1 th:text="'hello world'"></h1>
    </body>
</html>

Recommended Posts

[Java] Thymeleaf Basic (Spring Boot)
CICS-Run Java application-(4) Spring Boot application
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Spring Java
Spring Boot + Java + GitHub authentication login
Elastic Beanstalk (Java) + Spring Boot + https
Java --Jersey Framework vs Spring Boot
[Java] LINE integration with Spring Boot
Thymeleaf usage notes in Spring Boot
Handle Java 8 date and time API with Thymeleaf with Spring Boot
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
Java basic grammar
Java basic grammar
Java basic knowledge 1
Challenge Spring Boot
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Java] Basic structure
[Java] [Basic] Glossary
Spring Boot + Docker Java development environment construction
Java basic grammar
Java basic grammar
Spring Boot Form
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement paging function with Spring Boot + Thymeleaf
Spring Boot Memorandum
gae + spring boot
Spring Boot + Thymeleaf BootStrap installation method memo
Java exercises [Basic]
[Java] Spring DI ③
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Use Thymeleaf text template mode from Spring Boot
[Java / Spring Boot] Spring security ④ --Implementation of login process
Java tips-Create a Spring Boot project in Gradle
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Create your own Utility with Thymeleaf with Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
LINE Bot x Java (Spring Boot) construction procedure
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
java basic knowledge memo
[Java] Data type ①-Basic type
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot exception handling
Spring Boot Servlet mapping
Java basic date manipulation
Spring boot development-development environment-
Spring Boot learning procedure
Java basic naming conventions
Java learning memo (basic)
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics