Spring MVC Minimal Project Setup
Table of Contents:
- Introduction
- Project Setup
- Maven Dependencies
- Project Structure
- Spring MVC Minimal Setup
- MainController
- Views
- Conclusion
Introduction
Spring MVC (Model-View-Controller) is a framework that allows developers to build web applications with ease. It provides a clean separation of concerns by dividing the logic into models, views, and controllers. In this article, we will focus on setting up a minimal Spring MVC project, a perfect starting point for those new to Spring.
We will explain the steps involved in configuring a simple Spring MVC project, walk through the key files, and explain the functionality of the minimal controller.
Project Setup
To create a minimal Spring MVC application, we need to start by setting up the following structure and dependencies.
Maven Dependencies
We will use Maven to manage our dependencies. The pom.xml file contains the required dependencies, such as Spring Framework and Servlet API. Below is an example of the necessary dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 |
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> </dependencies> |
Project Structure
Our Spring MVC project will have the following structure:
- src/main/java: Contains the Java source code.
- src/main/webapp: Contains the web application resources (e.g., JSP or HTML files).
- pom.xml: The Maven configuration file.
Spring MVC Minimal Setup
MainController
The MainController.java file is the heart of our minimal Spring MVC setup. This controller is responsible for handling requests to the home page of our application. Below is the code for the MainController:
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.studyeasy; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MainController { @RequestMapping("/") public String home() { return "home"; } } |
Explanation:
- The @Controller annotation indicates that this class serves the role of a web controller in Spring MVC.
- The @RequestMapping(“/”) annotation maps HTTP requests to the “/” path, which in this case is the root of the application.
- The home() method returns a string “home”, which refers to a view called home.jsp or home.html, depending on your view resolver configuration.
This minimal setup allows the Spring MVC application to display the “home” view when the root URL of the web application is accessed.
Views
The view file, typically located in the src/main/webapp directory, displays the content to the user. For example, if we are using JSP as our view technology, the home.jsp file would look like this:
1 2 3 4 5 6 7 8 9 10 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Home Page</title> </head> <body> <h1>Welcome to the Spring MVC Application!</h1> </body> </html> |
This JSP file is returned when a user accesses the root URL.
Conclusion
In this article, we walked through setting up a minimal Spring MVC project. We covered the Maven dependencies, project structure, and a simple MainController that maps a request to a view. With this setup, you can easily expand the project by adding more controllers, services, and views to create a full-fledged web application.