Introduction to Spring MVC
Spring MVC is a lightweight web framework based on the MVC architecture model. The purpose is to modularize web development and decouple the overall architecture.
Spring MVC has the following advantages:
As part of the Spring framework, it has the advantages of Spring (IOC, AOP, etc.)
Supports flexible URL-to-page controller mapping
Provide flexible data verification, formatting, and data binding mechanisms
Support RESTful style
Spring MVC request process
The overall request process of Spring MVC framework is as follows:
The above figure involves several functional components of Spring MVC:
Front-end controller (DispatcherServlet): Receive user request and return the request result. Its function is equivalent to a forwarder or central processor, which controls the entire execution process, scheduling various components and reducing coupling between components.
Processor Mapping: Find the corresponding processor Handler based on the URL requested by the user, through annotation or XML configuration.
Processor Adapter: Complete the call to the method in the processor based on the Handler found by the mapper
Handler: the specific logic of request processing, returning data and view information
View Resolver: parses the specific view, and parses the logical view name into a real view through the View information in the ModelAndView object.
Detailed explanation of the specific steps of the request process:
1: The user initiates a request, and the request will be intercepted by the front-end controller (DispatcherServlet)
2: Front-end controller (DispatcherServlet) request processor mapper (HandlerMapping) to find Handler
3: The processor mapper (HandlerMapping) finds the corresponding Handler according to the configuration (can be more annotated or XML configuration), which may contain multiple Interceptor interceptors and return them to the front-end controller.
4: The front-end controller (DispatcherServlet) requests the processor adapter (HandlerAdapter) to execute the corresponding Handler
5: The adapter is handed over to the corresponding Handler processor to execute
6: After the Handler processor is executed, return the ModelAndView object to the processor adapter
7: The processor adapter accepts the return result of the Handler processor and returns the result to the front-end controller (DispatcherServlet)
8: The front-end controller (DispatcherServlet) receives the data and view information returned by the processor adapter, requests the view parser, and parses the corresponding view
9: The view parser returns to the front-end controller based on the corresponding view result matching the View information.
10: The front-end controller receives the specific view, renders the view, fills the Model data into the View view, and generates the final view
11: The front-end controller returns the result to the user
Build demos from scratch
Create a project:
Create a new dynamic web project under Eclipse
Project default directory structure:
Add jar package dependencies
Import the corresponding jar package under WebContent > WEB-INF > lib folder, where the core jar package is spring-webmvc-5.0.0.RELEASE.jar, and the others are mainly spring for managing context and beande packages, jstl tag library and a log package for printing logs:
Configuring the front-end controller in web.xml
The front-end controller is equivalent to Spring MVC's proprietary servlet, used to intercept all qualifying requests and hand them over to the framework for subsequent processing.
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- Configure front-end controller-DispatchServlet --> <servlet> <servlet-name>springMvcNext</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation is not required. If contextConfigLocation is not configured, the configuration file of springmvc is defaulted to: WEB-INF/servlet name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMvcNext</servlet-name> <url-pattern>/</url-pattern> <!--Intercept requests sent by users according to the rules set by url-pattern. All requests are intercepted here, including static resources-> </servlet-mapping> </web-app>
The url matching rule defined in the <servlet-mapping> tag is in a form that conforms to *.action, the corresponding servlet is named springMvcNext, and the controller configured by <servlet is org.springframework.web.servlet.DispatchServlet, which is the front-end controller of the current SpringMVC project. The <init-param> tag is the parameter that the current controller depends on. The two parameters represent the context parameters and the parameter loading path respectively.
About classpath: represents the output path after compilation of web project
Configure spring MVC configuration
Add applicationContext.xml file in the Java source code directory
Specific content:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="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/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- The package scanner tag will be used to activate the Spring MVC annotation scanning function, allowing annotations such as @Controller and @RequestMapping. --> <context:component-scan base-package="com.sl.controller" /> <!-- Annotation driver --> <mvc:annotation-driven /> <!-- Configure view resolver --> <bean id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean></beans>
Add Controller and View View
Add the package com.sl.controller in the Src directory, and add the controller code as follows:
package com.sl.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class HelloWorldController { @RequestMapping("/index") //handle all requests in the URL path starting with /index: including /index/* and /index.html public ModelAndView helloWorld() { String message = "Hello Spring MVC"; return new ModelAndView("index", "message", message); }}Add view file index.jsp in WEB-INF/view
<html><head> <title>Spring MVC </title></head><body> ${message}</body></html>Running results:
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.