English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring MVC is a Java framework used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic functions of the core Spring framework, such as inversion of control, dependency injection.
Spring MVC through DispatcherServlet It provides an elegant solution to use MVC in the Spring framework. >. Here, DispatcherServlet It is a class that receives incoming requests and maps them to the correct resources, such as controllers, models, and views.
Model-The model contains the data of the application. The data can be a single object or a collection of objects. Controller-The controller contains the business logic of the application. Here, the @Controller annotation is used to mark the class as a controller. View-Views represent the provided information in a specific format. Typically, JSP + JSTL is used to create view pages. Although spring also supports other view technologies, such as Apache Velocity, Thymeleaf, and FreeMarker. Front Controller-In Spring Web MVC, the DispatcherServlet class is used as a front controller. It is responsible for managing the process of the Spring MVC application.
As shown in the figure, all incoming requests are intercepted by the DispatcherServlet of the front-end controller. DispatcherServlet retrieves the handler mapping entries from the XML file and forwards the request to the controller. The controller returns a ModelAndView object. DispatcherServlet checks the view resolver entries in the XML file and calls the specified view component.
Let's look at some advantages of the Spring MVC Framework: -
Separation of roles-Spring MVC separates each role, where model objects, controllers, command objects, view resolvers, DispatcherServlet, validators, and others can be implemented by specialized objects. Lightweight-It uses a lightweight servlet container to develop and deploy your application. Powerful configuration-It provides reliable configuration for the framework and application classes, including easy cross-context references, such as from web controllers to business objects and validators. Rapid development-Spring MVC promotes rapid and parallel development. Reusable business code-No need to create a new object, it allows us to use existing business objects. Easy to test-In Spring, we usually create JavaBeans classes to allow you to inject test data using setter methods. Flexible mapping-It provides specific annotations for easy page redirection.
Let's look at a simple example of the Spring Web MVC framework. The steps are as follows:
Load the Spring jar file or add the dependency in Maven Create the controller class Provide the controller entry in the web.xml file Define beans in a separate XML file Display the message on the JSP page Start the server and deploy the project
To run this example, you need to load:
Spring Core jar file Spring Web jar file JSP + JSTL jar file (if using other view technologies, load the corresponding jar file).
Download all the jar files of Spring, including JSP and JSTL.
If you are using Maven, there is no need to add jar files. Now, you need to add Maven dependencies to the pom.xml file.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.w3codebox</groupId> <artifactId>SpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringMVC Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-<artifactId>api</artifactId> <version>3.0-alpha-1</version> </dependency> </dependencies> <build> <finalName>SpringMVC</finalName> </build> </project>
To create a controller class, we use two annotations @Controller and @RequestMapping.
@Controller annotation marks this class as a Controller.
@RequestMapping annotation is used to map classes with specified URL names.
HelloController.java
package com.w3codebox; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/) public String display() { return "index"; } }
In this xml file, we specify the Servlet class DispatcherServlet as the front controller in Spring Web MVC. All incoming requests to html files will be forwarded to DispatcherServlet.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
This is an important configuration file where View components need to be specified.
context: component-scan element defines the basic package of DispatcherServlet. It will search for controller classes.
This xml file should be located in the WEB-INF directory.
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- Provide support for component scanning --> <context:component-scan base-package="com.w3codebox" /> <!--Provide support for conversion, formatting and validation --> <mvc:annotation-driven/> </beans>
This is a simple JSP page that displays the message returned by the Controller.
index.jsp
<html> <body> <p>Welcome to Spring MVC Tutorial</p>/p> </body> </html>
Output:
Spring MVC
Spring MVC Forms
Spring MVC Application
Spring MVC Validation
Spring MVC Tiles