Hello World application in spring boot using controller and thymeleaf
Hello, World is one of the first application we always refer whenever we start learning any programming language or any concept and spring is no different, In the article, we will show you how to create simple Hello World application in Spring boot using Spring MVC and Thymeleaf page.
#Objective
We need to simply create a hello world program in spring boot using spring MVC and Thymeleafpage.
#Solution
To Achieve this we will first create the Spring boot application using start.spring.io
Now follow below code snippet.
Dependencies used
Spring Web Starter
Thymeleaf Starter
Implementation Strategy
We will first create one controller as HelloWorldController ( which is a spring MVC Controller ).
Then we will create one Thymeleaf Page where we are basically showing Welcome To Spring Boot Project.
We will connect both the Controller to View.
Also Read : Top 5 Spring boot Courses for Developers
Implementation( You can download the Project from GitHub)
The Project Structure
Lets start from the pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>in.learnspringboot</groupId> <artifactId>HelloWorldSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>HelloWorldSpringBoot</name> <description>Hello World Spring boot </description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Now let see the Controller class
package in.learnspringboot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloWorldController { // url to access this contoller is // http://localhost:8080/home @GetMapping("/home") public String doHome( @RequestParam(value="name",required = false) String name,Model model) { if(name!=null ) { model.addAttribute("name", name); }else { model.addAttribute("name", "Spring boot User"); } return "index"; } }
In above code
@Controller
Enables class to be Controller class
@GetMapping : this annotation is used for Get Request and the request mapping to home controller
@RequestParam : this annotation is for the QueryParam value to get from the url
Now we will see the HTML file
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="ISO-8859-1"> <title>Welcome</title> </head> <body> <p>Welcome to Spring boot Project , <span th:text="${name}"/></p> </body> </html>
Here th:text=${name} this basically fetch the data from the model attribute set in the controller.
Points to remember
- In Spring MVC when we call the Thymeleaf page, we basically call the URI of the Controller which is of Get type which will then call the view.
- The view of the always stored in template folder
- there is one another folder called static which is used for images and other static files.
Let see how to call the html page.
URL TO CALL
http://localhost:8080/home?name=vishal
OR
http://localhost:8080/home
This first case the result will be Welcome to Spring boot Project , vishal
In Second Case the Result will be
Welcome to Spring boot Project , Spring Boot User
Hope this will help you in writing your first hello world spring boot application.
Very well explained, it is an apt example for thymleaf integration with Spring Boot.
Only 1 small change is needed in index.html, we need to give an end tag, maybe it was an issue with my app.
Welcome
Welcome to Spring boot Project ,