一、DispatcherServlet宏观流程
1、请求被DispatherServlet拦截
2 、HandlerMapping为处理器映射,DispatcherServlet调用,HandlerMapping,HandlerMapping根据请求url查找Handler(处理器)
3、HandlerExecution表示具体Handler,其主要作用是根据url查找控制器,如上url被查找控制器
4、HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。
5、Handler让具体的Controller执行。
6、Controller让具体的执行信息返回给HandlerAdapter,如ModelAndView
7、HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet
8、DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名
9、视图解析器将解析的逻辑视图名传给DispatcherServlet。
10、DispatcherServlet根据视图解析器解析的视图结果,调用具体的视图。
11、最终视图呈现给用户
我个人的理解:
一个请求请求进来,被DispatcherServlet进行拦截,然后映射给HandlerMapping,由HandlerMapping找到Controller的id,然后返回给DispatcherServlet,然后由DispatcherServlet交给了HandlerAdapter,找到具体的controller即可,再调用具体的业务,然后返回给DispatcherServlet,将数据封装到VidewResolver,然后返回给前端。
SpringMvc将代码web代码运行过程封装成一个个的对象
二、SpringMVC的两种使用方式:配置文件,注解
1)配置文件方式
(1)配置文件中的web.xml的配置
-
<?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_4_0.xsd"
-
version=
"4.0">
-
<!--1.注册servlet-->
-
<servlet>
-
<servlet-name>SpringMVC
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
-
<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>
-
-
<!--所有请求都会被springmvc拦截 -->
-
<servlet-mapping>
-
<servlet-name>SpringMVC
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
</web-app>
在web.xml中进行配置DispatcherServlet,首先当请求请求过来的时候,会被拦截器进行拦截,然后调用DispatcherServlet,由DispatcherServlet然后调用Springmvc-servlet.xml中的映射器
(2)springmvc-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
-
https://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/mvc
-
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
<!-- 添加处理映射器-->
-
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
-
<!-- 添加处理器适配器-->
-
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
-
-
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
-
<!--前缀-->
-
<property name="prefix" value="/WEB-INF/jsp/"/>
-
<!--后缀-->
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
-
<!-- Handler-->
-
<bean id="/hello" class="com.kuang.controller.HelloController"/>
-
-
</beans>
由映射器进行映射,url和Controller,然后再通过适配器调取handler
(3)Controller
-
package com.kuang.controller;
-
-
import org.springframework.web.servlet.ModelAndView;
-
import org.springframework.web.servlet.mvc.Controller;
-
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
public
class HelloController implements Controller {
-
@Override
-
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
-
-
ModelAndView mv =
new ModelAndView();
-
//业务代码
-
String result =
"HelloSpringMVC";
-
mv.addObject(
"msg",request);
-
//视图跳转(页面的名字)
-
mv.setViewName(
"test");
-
return mv;
-
}
-
}
2)注解的方式
(1)web.xml
-
<?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_4_0.xsd"
-
version=
"4.0">
-
<!--1.注册servlet-->
-
<servlet>
-
<servlet-name>SpringMVC
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
-
<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>
-
-
<!--所有请求都会被springmvc拦截 -->
-
<servlet-mapping>
-
<servlet-name>SpringMVC
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
</web-app>
(2) springmvc-servlet
-
<?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">
-
-
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
-
<context:component-scan base-package="com.kuang.controller"/>
-
<!-- 让Spring MVC不处理静态资源 .css .js .html-->
-
<mvc:default-servlet-handler />
-
<!--
-
支持mvc注解驱动
-
在spring中一般采用@RequestMapping注解来完成映射关系
-
要想使@RequestMapping注解生效
-
必须向上下文中注册DefaultAnnotationHandlerMapping
-
和一个AnnotationMethodHandlerAdapter实例
-
这两个实例分别在类级别和方法级别处理。
-
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-
-->
-
<mvc:annotation-driven />
-
-
<!-- 视图解析器 -->
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
-
id=
"internalResourceViewResolver">
-
<!-- 前缀 -->
-
<property name="prefix" value="/WEB-INF/jsp/" />
-
<!-- 后缀 -->
-
<property name="suffix" value=".jsp" />
-
</bean>
-
-
</beans>
(3) controller
-
-
@Controller
-
@RequestMapping("/hello")
-
public
class HelloController {
-
-
@RequestMapping("/h1")
-
public String hello(Model model){
-
//封装数据
-
model.addAttribute(
"msg",
"Hello,SpringMVCAnnotation");
-
return
"hello";
//会被视图解析器处理
-
}
-
}
转载:https://blog.csdn.net/MyxZxd/article/details/116427917