Architecture of Spring MVC: Spring MVC is a Java framework used to build web applications. Spring MVC is a model view controller-based web framework inside the Spring framework in Java. It provides ready-to-use functionalities to the developers. The architecture of Spring MVC consists of four major components, Model, View, Controller, and DispatcherServlet Class. Let us know more about Spring MVC and its architecture in this article.
What is Spring MVC?
Spring MVC is a model view controller-based Java web framework that makes powerful web applications. It provides ready-to-use functionalities. MVC stands for Model, View, and Controller. Spring MVC makes it easy for developers to start and configure according to their needs.
What is a Model View Controller (MVC)?
Model-View-Controller is a design architecture that organizes Java applications in different distinct layers. Each of these layers has distinct functions and tasks. Different web languages such as Java, Python, Javascript, PHP, and C# are used to build applications on MVC. Some popular MVC frameworks are Ruby on Rails, Django, CherryPy, Django, etc.
MVC allows developers to reuse components and build independent components simultaneously. The distinct layers in MVC can also interact with each other to ensure proper coordination. MVC allows faster and more efficient development of complex web applications.
Also Read: Spring Boot Rest API: Rest APIS with Java and Spring Boot
Architecture and Framework of Spring MVC
DispatchServlet is used to create web applications in MVC. DispatchServlet is a class that receives incoming application requests and connects them to the appropriate layer. Let us understand the architecture of the Spring MVC framework in this article.
- Model: A model is a single object or collection of data. The model layer is responsible for storing and retrieving the data from the backend. It performs data-related tasks and ensures its integrity and accessibility to its users.
- View: This layer provides the user interface necessary for ease of interaction with the application. Some of the frequently used view components in the spring MVC are tables, drop-downs, buttons, links, text, etc. It ensures that the format of the applications is well organized.
- Controller: The controller layer consists of the application logic to implement proper communications across the application. It acts as an interface between the view and model layer in MVC. It can be implemented with the @controller notation to assign the class as the coordinator.
- DispatchServlet Class: They are also known as the front controller in Spring MVC. It is used to manage the flow of the spring MVC application.
Architecture of Spring MVC: Flow of Spring MVC
Let us now check the architecture flow of spring MVC using a suitable diagram below.
- DispatchServelet manages and controls the flow of applications in Spring MVC. It is used to intercept all the incoming requests and is hence known as the front controller.
- The dispatch servelet then connects with the handler mapping in the XML file and then forwards the incoming requests to the controller.
- The controller then returns the Model and View objects. Controllers in MVC are annotated using @Controller. The methods in the controller handle the request using the appropriate method.
- Now, the model is fed with the required data, which can be used to pass the information between the view and controller.
- The controller returns the view name, which is converted into the actual view implementation.
- The HTML view content is sent back as the response in a proper format using DispatchServlet.
Architecture of Spring MVC: Examples
Let us implement a complete basic application using MVC to handle requests from URLs and then provide a response after processing.
Also Read: Spring Boot Tutorial
Spring Controller Class
Save the Java file as HelloWorldController.java. Import the required modules and controller framework. Check out the Java implementation below.
Architecture of Spring MVC: Controller Class |
package com.example.controller;
import com.example.model.Message; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloWorldController { @RequestMapping(value = “/hello”, method = RequestMethod.GET) public String hello(Model model) { Message message = new Message(“Hello, Spring MVC!”); model.addAttribute(“message”, message); return “hello”; } } |
2. Spring MVC Model Class
Architecture of Spring MVC: Model Class |
package com.example.model;
public class Message { private String content; public Message(String content) { this.content = content; } public String getContent() { return content; } } |
3. Spring MVC View Layer
It is saved with a .jsp extension and is used to render and handle the presentation of the data. It defines the architecture using HTML for sending the response back after processing.
Architecture of Spring MVC: Spring MVC View Layer |
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<html> <head> <title>Hello Spring MVC</title> </head> <body> <h2>${message.content}</h2> </body> </html> |
4. Spring Configuration Class
Architecture of Spring MVC: Configuration Class |
package com.example.web;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @EnableWebMvc @ComponentScan(basePackages = “com.example”) public class AppConfig { } |
5. Spring XML Web Deployment Descriptor
Architecture of Spring MVC: Web Deployment Descriptor |
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” version=”3.1″> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
6. Spring MVC Dispatcher Servlet XML
Architecture of Spring MVC: Dispatcher Servlet XML |
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:context=”http://www.springframework.org/schema/context” xmlns:mvc=”http://www.springframework.org/schema/mvc” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd”> <context:component-scan base-package=”com.example”/> <mvc:annotation-driven/> <mvc:view-resolvers> <mvc:jsp suffix=”.jsp” /> </mvc:view-resolvers> </beans> |
Use the local host to display the response. Find the URL ‘http://localhost:8080/your-web-context/hello’.
Also Read: Architecture Of Spring Boot – Examples, Patterns, Diagram
Advantages of Spring MVC
Spring MVC is widely used for building various web applications because of its ready-to-use components and ease of use by developers. Let us check out some of the major benefits of using MVC.
- The spring MVC role consists of various layers consisting of distinct tasks and roles to provide more flexibility.
- It is a lightweight container used to develop and deploy web applications.
- It facilitates fast and effective development.
- It also allows reusable components for making robust applications.
- It provides validation and customization options for greater flexibility.
- The latest version of Spring MVC provides support for Restful web services using @RestController.
- Testing components and dependency in MVC is easy. Most of the testing frameworks such as unit testing, integration testing, etc can easily be implemented.
Learn Java with DSA
Enrol in our Java with DSA Course to learn different aspects of Java programming with Data Structures and algorithms. Make yourself ready for roles such as Software Developer, Software Engineer, Java Developer, etc. The course will help you learn important skills such as Java programming, data structure, code optimization, Github, etc.
Learn and practise with real-time industry-relevant projects along with free PW Lab access. Hurry and avail of exciting benefits only at @pwskills.com
For Latest Tech Related Information, Join Our Official Free Telegram Group : PW Skills Telegram Group
Architecture of Spring MVC
What is a spring MVC?
Spring MVC is a model view controller-based Java web framework that makes powerful web applications. It provides ready-to-use functionalities.
What are the different layers of Spring MVC?
The Spring MVC framework consists of four different layers namely, model, view, controller, and dispatcher servlet.
What are the different types of Spring MVC architecture?
The different types of spring MVC architecture are given below.
Hierarchical model view controller (HMVC)
Model View Adapter (MVA)
Model View Presenter (MVP)
Model View ViewModel (MVVM)