◆Home Improvement Ideas Journal

Spring MVC Project Tutorial: Step-by-Step Example for Beginners

By

Spring MVC is a popular Java web framework for building highly scalable and easily testable web applications. It separates the model, view, and controller into different parts, allowing developers to create clean, loosely coupled code. Let's delve into a comprehensive example of a Spring MVC project, exploring its key components and workflow.

At the core of a Spring MVC application lies the controller. It handles user requests and decides which view to render or data to process. Let's discuss two types of controllers: BeanNameUrlHandlerMapping and RequestMappingHandlerMapping.

Controllers in Spring MVC

The BeanNameUrlHandlerMapping is a simple controller that uses the bean name as the URL. It maps requests based on the bean's ID in the application context. To use it, annotate your controller with @Controller and don't include any request mappings.

Spring Mvc Template
Spring Mvc Template
Spring MVC Hibernate MySQL Integration CRUD Example Tutorial | DigitalOcean
Spring MVC Hibernate MySQL Integration CRUD Example Tutorial | DigitalOcean
Spring MVC beginner tutorial with Spring Tool Suite IDE
Spring MVC beginner tutorial with Spring Tool Suite IDE

On the other hand, RequestMappingHandlerMapping is more flexible, allowing developers to map URLs to specific handlers using annotations like @GetMapping, @PostMapping, and @RequestMapping. Let's see an example:

Using @RequestMapping

Create a simple controller with a method to handle both GET and POST requests:

Spring MVC with Hibernate CRUD example
Spring MVC with Hibernate CRUD example
Complete Spring Framework Cheat Sheet | Spring Boot, MVC
Complete Spring Framework Cheat Sheet | Spring Boot, MVC
a diagram showing the different types of devices that can be used to monitor and perform tasks
a diagram showing the different types of devices that can be used to monitor and perform tasks
```java @Controller public class MyController { @RequestMapping(value = "/home", method = {RequestMethod.GET, RequestMethod.POST}) public String home(@RequestParam(value = "name", defaultValue = "World") String name, Model model) { model.addAttribute("message", "Hello, " + name + "!"); return "home"; } } ```

The @RequestMapping annotation maps the URL "/home" to this method. The @RequestParam annotation retrieves the 'name' parameter from the request.

Rendering Views

Spring MVC uses ViewResolver to determine what view to render. TheThymeleaf templating engine is a popular choice. To use it, add these dependencies to your pom.xml:

```xml org.springframework.boot spring-boot-starter-thymeleaf ```

Then, in your controller, add a model with data and return the view name:

Spring MVC Hello World Example
Spring MVC Hello World Example
What is the MVC Pattern? A Beginner’s Guide to Clean Architecture
What is the MVC Pattern? A Beginner’s Guide to Clean Architecture
a diagram with the words model and user
a diagram with the words model and user
2.2. Overview of Spring MVC Architecture — TERASOLUNA Global Framework Development Guideline 1.0.1.RELEASE documentation
2.2. Overview of Spring MVC Architecture — TERASOLUNA Global Framework Development Guideline 1.0.1.RELEASE documentation
Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat Server
Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat Server
a diagram showing the flow of data from different sources to each other, with arrows pointing up and down
a diagram showing the flow of data from different sources to each other, with arrows pointing up and down
```java @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Welcome to my app!"); return "index"; } ```

The "index.html" file in the src/main/resources/templates folder will be rendered with the specified message. Now, let's explore the Model-view aspect of Spring MVC.

Model-View Separation

Spring MVC promotes a clear separation of concerns between the model and the view. The model contains the data, and the view decides how to represent it. Let's create a simple model and update our controller to use it.

Creating a Model

Create a new class Greeting.java representing the model:

a screenshot of a computer screen with the text field highlighted
a screenshot of a computer screen with the text field highlighted
Spring boot MVC Project to Upload & Download File with Example | #springboottutorial
Spring boot MVC Project to Upload & Download File with Example | #springboottutorial
Spring Boot MVC & REST Annotations With Examples
Spring Boot MVC & REST Annotations With Examples
How to create a Java Web application using Spring MVC Framework? Example Tutorial
How to create a Java Web application using Spring MVC Framework? Example Tutorial
20 Spring Boot Projects Every Java Developer Should Build | Real-World Backend Project Ideas
20 Spring Boot Projects Every Java Developer Should Build | Real-World Backend Project Ideas
How to use @ModelAttribute in Spring MVC? Example Tutorial
How to use @ModelAttribute in Spring MVC? Example Tutorial
An Introduction to MVC Architecture: Examples Explained
An Introduction to MVC Architecture: Examples Explained
MVC Architecture and Framework with Examples
MVC Architecture and Framework with Examples
the spring logo is shown on a green background
the spring logo is shown on a green background
MVC Architecture
MVC Architecture
an image of a diagram with the words spring m vc request lifecycle on it
an image of a diagram with the words spring m vc request lifecycle on it
Spring Boot With JSP and Spring MVC | Java Inspires
Spring Boot With JSP and Spring MVC | Java Inspires
What is MVC Architecture?
What is MVC Architecture?
Top 30 Spring Core, Spring MVC and Spring Security Interview Questions Answers
Top 30 Spring Core, Spring MVC and Spring Security Interview Questions Answers
```java public class Greeting { private long id; private String content; // Getters and setters } ```

Update the controller to use this model:

```java @Controller public class GreetingController { private int count = 0; @GetMapping("/greeting") public String greetingForm(Model model) { model.addAttribute("greeting", new Greeting()); return "greeting"; } @PostMapping("/greeting") public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) { model.addAttribute("greeting", greeting); model.addAttribute("message", "Hello, " + greeting.getContent() + "!"); return "result"; } } ```

The @ModelAttribute annotation binds form data to a Greeting object. The view ("greeting.html") displays the form, and the view ("result.html") shows the result.

Spring MVC's powerful features and separation of concerns make it an ideal choice for enterprise-scale web applications. From simple controllers to complex data binding, Spring MVC excels in flexibility and ease of use. So, why not give it a try in your next project?