Development

스프링 부츠에서 Thymeleaf를 사용하는 방법

sonpro 2023. 3. 12. 22:10
반응형

thymeleaf

How to Use Thymeleaf in Spring Boot

Thymeleaf is a powerful Java-based template engine that is used to develop web applications. It is a server-side technology that allows developers to create dynamic web pages using HTML, CSS, and JavaScript. Thymeleaf is widely used in the Spring Boot framework to create web applications. In this blog post, we will discuss how to use Thymeleaf in Spring Boot.

What is Thymeleaf?

Thymeleaf is a Java-based template engine that is used to create dynamic web pages. It is an open-source technology that is widely used in web development. Thymeleaf allows developers to create web pages using HTML, CSS, and JavaScript. It is a server-side technology that is used to generate HTML pages dynamically.

Why Use Thymeleaf in Spring Boot?

Spring Boot is a popular Java-based framework that is used to create web applications. Thymeleaf is widely used in Spring Boot to create dynamic web pages. Thymeleaf is easy to use and provides a lot of features that make web development easier. Thymeleaf is also very flexible and can be used with different web frameworks.

How to Use Thymeleaf in Spring Boot?

To use Thymeleaf in Spring Boot, you need to follow these steps:

  1. Add Thymeleaf Dependency

The first step is to add the Thymeleaf dependency to your Spring Boot project. You can add the Thymeleaf dependency to your project by adding the following code to your pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. Create a Thymeleaf Template

The second step is to create a Thymeleaf template. A Thymeleaf template is an HTML file that contains Thymeleaf tags. Thymeleaf tags are used to create dynamic content in the HTML file. To create a Thymeleaf template, you need to create an HTML file and add Thymeleaf tags to it. Here is an example of a Thymeleaf template:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf Example</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

In the above example, we have created a Thymeleaf template that contains a message variable. The message variable is set using the th:text attribute.

  1. Create a Controller

The third step is to create a controller. A controller is a Java class that is used to handle HTTP requests. To create a controller, you need to create a Java class and annotate it with the @Controller annotation. Here is an example of a controller:

@Controller
public class HomeController {

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

In the above example, we have created a controller that handles the root URL ("/"). The controller sets the message variable and returns the name of the Thymeleaf template ("home").

  1. Run the Application

The final step is to run the application. To run the application, you need to use the Spring Boot Maven plugin. You can run the application using the following command:

mvn spring-boot:run

Once the application is running, you can access it using a web browser. When you access the application, you should see the message "Hello, World!".

Conclusion

Thymeleaf is a powerful Java-based template engine that is widely used in web development. It is easy to use and provides a lot of features that make web development easier. Thymeleaf is also very flexible and can be used with different web frameworks. In this blog post, we discussed how to use Thymeleaf in Spring Boot. We hope this blog post was helpful in getting you started with Thymeleaf in Spring Boot.

반응형