Spring MVC principle and configuration
1. Spring MVC overview
Spring MVC It is a powerful and flexible web framework provided by Spring. With the help of annotations, Spring MVC provides a development model that is almost POJO, making the development and testing of the controller easier. These controllers generally do not directly process requests, but delegate them to other beans in the Spring context. These beans are injected into the controller through Spring’s dependency injection function.
Spring MVC is mainly composed of DispatcherServlet, processor mapping, processor (controller), view resolver, and view.
2. SpringMVC operation principle
3.SpringMVC interface explanation
3.1 DispatcherServlet interface
Spring provided The front controller, all requests are distributed uniformly through it. Before DispatcherServlet distributes the request to Spring Controller, it needs to locate the specific Controller with the help of HandlerMapping provided by Spring.
3.2 HandlerMapping interface
It can complete the mapping of client requests to Controller.
3.3 Controller interface
The above request needs to be processed for concurrent users. Therefore, when implementing the Controller interface, it must be thread-safe and reusable.
Controller will process user requests, which is consistent with the role played by Struts Action. Once the Controller has processed the user request, it returns the ModelAndView object to the DispatcherServlet front controller, and ModelAndView contains the model (Model) and the view (View).
From a macro perspective, DispatcherServlet is the controller of the entire Web application; from a micro perspective, Controller is the controller in the processing of a single Http request, and ModelAndView is the model and view (View) returned during the Http request. ).
3.4 ViewResolver interface
The view resolver (ViewResolver) provided by Spring finds the View object in the web application and renders the corresponding result to the client.
4. DispatcherServlet
is the core of the entire Spring MVC. It is responsible for receiving HTTP requests, organizing and coordinating the various components of Spring MVC. Its main tasks are as follows:
- Intercept URL requests that conform to a specific format.
- Initialize the DispatcherServlet context corresponding to the WebApplicationContext, and associate it with the WebApplicationContext of the business layer and the persistence layer.
- Initialize the components of Spring MVC and assemble them into DispatcherServlet.
5. Example
5.1 Configure web.xml file
xml version="1.0" encoding="UTF-8"?>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_4_0.xsd"
version="4.0">
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation < br />classpath:springmvc.xml
1 < br />
springmvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
init-param>
CharacterEncodingFilter
/* url-pattern>
5.2 Configure springmvc.xml file
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/mvc http ://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
< /property>
As the above two main configuration files are successfully configured, the following code is written
5.3 main code preparation
-
reg.jsp
<%@ page contentType="text/html;charset=UTF- 8" language="java" %>
User registration success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
Title
Registered successfully
The user information is as follows:
h5>
Name: ${user.name}
Age: ${user.age}
Password: ${user.pwd}