Spring Boot MVC Hello World Example with Thymeleaf

Introduction:

In this blog post, we will explore a simple and practical example of building a Hello World web application using Spring Boot MVC and Thymeleaf. Spring Boot MVC provides an elegant and efficient way to develop web applications, while Thymeleaf is a popular templating engine that allows for server-side rendering of dynamic content. By combining these technologies, we can create a robust and interactive web application effortlessly. So, let's get started with the Hello World example!

Prerequisites:

Before we begin, make sure you have the following prerequisites installed:

1. Java Development Kit (JDK) - version 8 or above.
2. Apache Maven - build automation tool for Java projects.
3. Integrated Development Environment (IDE) of your choice. (Eclipse, IntelliJ IDEA, or Spring Tool Suite)

Step 1: Set Up a New Spring Boot Project

To create a new Spring Boot project, follow these steps:

1. Open your IDE and choose to create a new Maven project.
2. Select the appropriate options for your project, such as GroupId, ArtifactId, and Java version.
3. Add the Spring Boot Starter Web and Thymeleaf dependencies in your project's `pom.xml` file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

4. Save the changes, and your project will be ready with the required dependencies.

Step 2: Create a Controller Class

Next, we'll create a controller class to handle the HTTP requests and render the Hello World view.

1. Create a new Java class under the package `com.example.controller`. For example, `HelloController.java`.
2. Annotate the class with `@Controller` to mark it as a controller component.
3. Create a method inside the class to handle the root URL ("/") request mapping:

@Controller
public class HelloController {

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

Step 3: Create a Thymeleaf Template

Now, let's create a Thymeleaf template that will be rendered when the root URL is accessed.

1. Create a new directory named `templates` under the `src/main/resources` directory.
2. Inside the `templates` directory, create a new HTML file named `hello.html`.
3. Add the following content to the `hello.html` file:

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

Step 4: Run the Application

Finally, let's run our Spring Boot application and see the Hello World message in action.

1. Open the main class of your project. By default, it is usually named `Application.java`.
2. Run the `main` method or use the IDE's built-in run configuration to start the application.
3. Once the application is started, open your web browser and visit `http://localhost:8080`.
4. Voila! You should see the "Hello, World!" message rendered on the webpage.

Conclusion:

Congratulations! You have successfully created a Spring Boot MVC Hello World example with Thymeleaf. We learned how to set up a Spring Boot project, create a controller to handle requests, and use Thymeleaf to render dynamic content. This simple example demonstrates the power and simplicity of Spring Boot and Thymeleaf in building web applications. From here, you can explore further and extend your application with additional features and functionality.

Remember to always experiment and explore the Spring Boot and Thymeleaf documentation to unlock more advanced capabilities and unleash your creativity. Happy coding!

Post a Comment

Previous Post Next Post