This article will explain how to create a web application using Spring boot step by step from scratch .
By the end of this article, you will be able to create a Spring boot web application that uses a Spring controller and HTML pages for serving web content.
Follow below steps to create a fresh Spring boot web project in Spring Tool Suite.
1. Creating project
Go to File -> New -> Spring Starter Project as shown below.
If you don’t find Spring Starter Project option, then select File -> New -> Project and then search for it.
2. Fill Details
Once you select Spring Starter Project, you will be presented with a list of options to fill out such as the build tool(Maven or Gradle), name of the project, Group, Artifact etc as shown below.
For this example, we will be using Maven.
If you are familiar with Maven, fill out the options as required.
If you are not familiar, then fill the name as the name of project, Artifact as springbootdemo
, Group and package as com.codippa.demo
.
Click Next.
3. Selecting modules
This is the most important step where you need to select Spring modules or dependencies we need to include in the project. Since we are creating a Spring Boot web application, we need Spring Web dependency.
In the text box, type web and select Spring Web checkbox as shown below.
Click Finish.
This will create a new project whose structure is as shown below.
Dependencies in pom.xml for this project are generated as below.
For a Spring boot web MVC application, only following dependency is required.
That is all. Spring boot Web MVC application development environment is now configured.
Starting App Development
If you have created a project following the above steps, then search for a class DemoApplication
or with the name you created the project.
This class would be located in the package name that you provided at the time of creating project and would look as below.
package com.codippa.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
This is the main class that Spring boot automatically creates.
If you run this class, it will start an embedded tomcat server listening at port 8080. This is evident from the startup logs below.
Open a browser and enter URL http://localhost:8080, you should see an error page as below.
This is because we have not provided a default page to display.
By default Spring boot MVC serves static content from
src/main/resources/static
folder and if you place a file with name index.html
at this location, it will automatically be considered as the welcome page when the root URL is accessed.Name of the file is significant.
Create
index.html
file at src/main/resources/static
folder with the below code. <h1>Learning Spring Boot</h1> <hr/> <h2>Hello World,</h2> <h2>This is my first Spring Boot MVC web application</h2>
Restart the application and now when you access http://localhost:8080 in the browser, you will see
Learning Spring Boot
Hello World,
This is my first Spring Boot MVC web application
What’s next?
Till now, you have successfully configured the development environment for a Spring boot web application which is the primary requirement before learning further.
This example does not use any view templates such as Thymeleaf or JSP and does not demonstrate using controllers to expose application URLs.
Follow Spring Boot MVC using Thymeleaf example to move ahead with learning Spring boot MVC application development using Thymeleaf.