3. Spring MVC implementation principle
The figure shows a relatively complete flow chart of spring MVC. The solid line indicates the technology provided by the spring MVC framework, which does not need to be implemented by developers, and the dotted line indicates that it needs to be implemented by developers.
Briefly analyze the execution process
-
Dispatcher servlet represents the front controller and is the control center of the whole spring MVC. The user sends a request, and the dispatcher servlet receives the request and intercepts the request.
Our request url is assumed to be: http://localhost:8080/SpringMVC/hello
As above, the url is divided into three parts:
http://localhost:8080 Server domain name
Spring MVC is a web site deployed on the server
hello indicates the controller
Through analysis, the above url is expressed as: request the hello controller of the spring MVC site located on the server localhost:8080.
-
HandlerMapping is processor mapping. DispatcherServlet calls HandlerMapping, which looks up the Handler according to the request url.
-
HandlerExecution refers to a specific Handler. Its main function is to find the controller according to the url. The controller found by the url above is: hello.
-
HandlerExecution passes the parsed information to DispatcherServlet, such as parsing controller mapping.
-
HandlerAdapter represents the processor adapter, which executes the Handler according to specific rules.
-
The Handler lets the specific Controller execute.
-
The Controller returns the specific execution information to the HandlerAdapter, such as ModelAndView.
-
The HandlerAdapter passes the logical name or model of the view to the dispatcher servlet.
-
DispatcherServlet calls the view resolver to resolve the logical view name passed by the HandlerAdapter.
-
The view parser passes the parsed logical view name to the dispatcher servlet.
-
DispatcherServlet calls the specific view according to the view result parsed by the view parser.
-
The final view is presented to the user.
4. Annotation development
1. Create a new Moudle, springmvc-03-hello-annotation. Add web support!
2. As Maven may have the problem of resource filtering, we will improve the configuration
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
3. In POM XML file introduces related dependencies: mainly Spring framework core library, Spring MVC, servlet, JSTL, etc. We have already introduced in parent dependency!
4. Configure web XML core file
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
/Difference between and / *: < URL pattern > / < / url pattern > will not match jsp, only for the requests we write; Namely: jsp will not enter the DispatcherServlet class of spring< URL pattern > / * < / url pattern > will match * jsp, when returning to the jsp view, it will enter the DispatcherServlet class of spring again, resulting in a 404 error because the corresponding controller cannot be found.
-
- Pay attention to the web XML version problem, to the latest version!
- Register DispatcherServlet
- The configuration file associated with spring MVC
- The startup level is 1
- The mapping path is / [do not use / *, it will 404]
5. Add Spring MVC configuration file
Add springmvc servlet. In the resource directory XML configuration file. The configuration form is basically similar to the Spring container configuration. In order to support annotation based IOC, the function of automatic package scanning is set. The specific configuration information is as follows:
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.wyz.controller"/> <!-- Give Way Spring MVC Do not process static resources --> <mvc:default-servlet-handler/> <!-- support mvc Annotation driven stay spring Generally used in@RequestMapping Annotation to complete the mapping relationship To make@RequestMapping Note effective Must register with context DefaultAnnotationHandlerMapping And one AnnotationMethodHandlerAdapter example These two instances are handled at the class level and method level respectively. and annotation-driven Configuration helps us automatically complete the injection of the above two instances.--> <mvc:annotation-driven/> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
In the view parser, we store all views in the / WEB-INF / directory, which can ensure the security of the view, because the files in this directory cannot be accessed directly by the client.
-
- Make IOC comments effective
- Static resource filtering: HTML JS . CSS . Pictures, videos
- Annotation driven MVC
- Configure view parser
6. Create Controller
@Controller public class HelloController { @RequestMapping("/hello") public String helloController(Model model){ model.addAttribute("msg","Hello SpringMVC Annotation!"); return "hello"; } }
- @The Controller is used to automatically scan the Spring IOC container during initialization;
- @RequestMapping is to map the request path. Here, because there are mappings on classes and methods, the access should be / HelloController/hello;
- The purpose of declaring Model type parameters in the method is to bring the data in the Action to the view;
- The result returned by the method is the name of the view hello, and the prefix in the configuration file becomes WEB-INF / JSP / hello jsp.
7. Create view
-
Create hello.jsp in the WEB-INF/ jsp directory JSP, the view can directly take out and display the information brought back from the Controller;
-
You can get the value or object stored in the Model through EL representation;
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
8. Run Tomcat test