?? Day 10: My Spring Boot Learning Journey ??

?? Day 10: My Spring Boot Learning Journey ??

Today, I created a Spring Boot Hello World Project using Thymeleaf, taking my first step toward building dynamic web applications!

Here’s what I accomplished:

To create a Spring Boot application and implement Thymeleaf templates, follow these steps:

1. Set up your Spring Boot project:

  • You can easily create a Spring Boot project using Spring Initializr or manually by creating a Maven or Gradle project.

Using Spring Initializr:

  1. Go to Spring Initializr.
  2. Choose your project metadata:

  • Project: Maven or Gradle (Maven is recommended for simplicity).
  • Language: Java
  • Spring Boot version: Choose the latest stable version.
  • Group: com.example
  • Artifact: thymeleaf-demo
  • Name: thymeleaf-demo
  • Packaging: Jar
  • Java Version: 11 or 17 (depending on your environment).

3. Under Dependencies, add:

  • Spring Web
  • Thymeleaf

4. Click Generate, and it will download a ZIP file of the project. Extract it and open it in your IDE (IntelliJ, Eclipse, VS Code).

Manually creating the project:

  1. pom.xml (for Maven):

<dependencies> <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> </dependencies>

2. Create a Controller to handle the views:

In src/main/java/com/example/thymeleafEG/Controller, create a Sampleprogram.java:

package com.example.thymeleafEG.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class Sampleprogram { @GetMapping("/hellos") public String hellos() { return"hellos"; } }

3. Create a Thymeleaf template:

Create a src/main/resources/templates/hellos.html file with Thymeleaf content:

<!DOCTYPE html> <html> <head> <title>Day1 springboot</title> </head> <body> <h6 th:text="'This is example for THYMELEAF'"></h6> </body> </html>

4. Run your Spring Boot Application:

In your main application class (ThymeleafEG.java):

package com.example.thymeleafEG; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ThymeleafEgApplication { public static void main(String[] args) { SpringApplication.run(ThymeleafEgApplication.class, args); } }

When the application runs successfully, it shows a massage in the console, as shown in the following figure.



5. Test the Application:

  1. Open your web browser and navigate to https://localhost:8080/hellos.


You should see the page displaying


Summary of the Steps:

  1. Create a Spring Boot application.
  2. Add dependencies (spring-boot-starter-web, spring-boot-starter-thymeleaf).
  3. Create a controller to handle web requests.
  4. Use Thymeleaf templates for dynamic content rendering.
  5. Run the application and test it.


要查看或添加评论,请登录

Sakthi Kumar M的更多文章

社区洞察

其他会员也浏览了