This guide walks you through building a simple Spring Boot web application using the MVC architecture. You will learn how to set up the project, create controllers, views, and handle web requests.
Install the following:
- Java 8+ (preferably 17 or 21)
- IDE (IntelliJ / STS / Eclipse)
- Maven or Gradle (Maven considered here)
- Web browser
Confirm Java:
java -versionGo to:
https://start.spring.io
Fill form:
-
Project: Maven
-
Language: Java
-
Spring Boot Version: stable latest
-
Project Metadata
- Group:
com.example - Artifact:
springboot-demo - Packaging:
Jar
- Group:
-
Java Version: 17 (or your LTS version)
Select:
- Spring Web
- Thymeleaf (for web pages support)
Click Generate, download the ZIP → extract → open in IDE.
src/
└── main/
├── java/
│ └── com.example.demo/
│ ├── DemoApplication.java
│ └── controller/
│ └── HomeController.java
└── resources/
├── static/
│ ├── css/
│ └── js/
├── templates/
│ └── home.html
└── application.properties
templates/→ HTML (Thymeleaf Views)static/→ CSS, JavaScript, imagesapplication.properties→ configurationcontroller/→ controller classesDemoApplication.java→ main entry point
DemoApplication.java is auto-generated:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}Explanation:
-
@SpringBootApplicationenables:- component scanning
- auto-configuration
- configuration properties
-
SpringApplication.run()starts:- Spring Context
- Embedded Tomcat server
- DispatcherServlet
Default app runs on port 8080
Create package:
com.example.demo.controller
Create file:
@Controller
public class HomeController {
@GetMapping("/")
public String homePage(Model model) {
model.addAttribute("title", "Spring Boot Web Project");
model.addAttribute("message", "Welcome! This is your first Spring Boot web page.");
return "home";
}
}@Controller→ marks as MVC controller@GetMapping("/")→ maps root URLModel→ carries data to view- returns view name →
home
Go to:
src/main/resources/templates/
Create file:
home.html
Add code:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}">Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
<p>This page is rendered using Spring Boot + Thymeleaf MVC.</p>
</body>
</html>Spring automatically uses Thymeleaf to render this.
No JSP needed.
Run DemoApplication.java
mvn spring-boot:runOpen in browser:
http://localhost:8080/
You will see your webpage rendered 🎯
@GetMapping("/user/{name}")
public String greetUser(@PathVariable String name, Model model) {
model.addAttribute("message", "Hello " + name);
return "home";
}URL:
/user/Ben
@GetMapping("/search")
public String search(@RequestParam String query, Model model) {
model.addAttribute("message", "Searching for: " + query);
return "home";
}URL:
/search?query=java
Add to home.html:
<form action="/submit" method="post">
<input type="text" name="name" placeholder="Enter Name">
<button type="submit">Submit</button>
</form>@PostMapping("/submit")
public String submit(@RequestParam String name, Model model) {
model.addAttribute("message", "Form submitted by: " + name);
return "home";
}This handles form submissions gracefully.
Spring Boot internally:
- configures DispatcherServlet
- registers it in embedded Tomcat
- routes every incoming HTTP request through it
Browser request
↓
DispatcherServlet
↓
HandlerMapping
↓
Controller method
↓
Returns Model + View
↓
ViewResolver selects template
↓
HTML generated → Browser
Located at:
src/main/resources/application.properties
server.port=9090server.error.whitelabel.enabled=falsespring.application.name=SpringBootWebAppPlace:
- CSS →
static/css - JS →
static/js - images →
static/images
Spring Boot serves them automatically.
Example:
<link rel="stylesheet" href="/css/style.css">
controllers → handle HTTP requests
models → hold data
services → business logic
repositories → DB operations (later with JPA)
templates → HTML UI
static → CSS/JS/images
| Issue | Fix |
|---|---|
| Port already in use | Stop previous server or change port |
| Template not found | check /templates/filename.html |
| Whitelabel Error Page | invalid controller or mapping |
| 404 Not Found | mapping incorrect |
| Method Not Allowed | using wrong HTTP method |
-
Spring Boot simplifies web app creation
-
MVC = Model + View + Controller
-
DispatcherServlet handles routing
-
Thymeleaf used for UI rendering
-
Minimal configuration required
-
Embedded server handles HTTP