Building a Boot Web App
Creating a web application structurally using Spring Boot requires almost zero manual configuration aside from establishing the project dependencies.
1. Project Setup
Most developers initialize a Spring Boot project utilizing the Spring Initializr (start.spring.io) website.
Select the following dependencies:
- Spring Web: Provides Spring MVC and the embedded Tomcat server.
- Thymeleaf: Provides the rendering engine cleanly mapping HTML templates.
2. Bootstrapping the Main Class
The initialized project provides a single foundational execution class.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyWebAppApplication {
public static void main(String[] args) {
// Launches the embedded Tomcat server dynamically
SpringApplication.run(MyWebAppApplication.class, args);
}
}
3. Creating the Controller
Place the controller in the same package structure as the main class so the @ComponentScan can find it safely.
@Controller
public class WebController {
@GetMapping("/dashboard")
public String loadDashboard(Model model) {
model.addAttribute("title", "Admin Control Panel");
// Maps to /src/main/resources/templates/dashboard.html
return "dashboard";
}
}