深入理解SpringMVC中央调度器DispatcherServlet

网友投稿 279 2022-08-07


深入理解SpringMVC中央调度器DispatcherServlet

目录SpringMVC请求处理过程:SrpingMVC容器和spring IOC容器关系基于maven创建的一个springmvc工程。1.创建maven项目中使用到了自动创建骨架2.配置java 文件 和资源文件3.添加jar包**4.springMVC项目web.xml初始化配置中央调度器以及自定义springmvc.xml文件web.xml:

springMVC是spring的一个模块,专门做web的。

SpringMVC请求处理过程:

请求发送,根据url-pattern,转发发送给中央调度器DispatcherServlet,中央调度器分配请求到自定义的Controller对象上,进行处理。

核心的就是中央调度器,所以我们在web.xml文件中要初始化配置中央调度器,中央调度器创建的同时,他会同时创建IOC springMVC IOC容器,用于管理对象的。 springmvc存放controller对象 view视图对象

如下是详情图:

HandlerMapping处理映射,作用是根据请求的url与controller里面的方法的@RequestMapping("/url") 的url比较 找到相应的Handler(方法)。

再详细版:

SrpingMVC容器和spring IOC容器关系

springmvc ioc容器是spring ioc容器的子容器。 SpringMVC的IOC容器中的bean可以来引用 Spring IOC 容器中的 bean. 返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!多个 Spring IOC 容器之间可以设置为父子关系,以实现良好的解耦。

关系图:

基于maven创建的一个springmvc工程。

1.创建maven项目中使用到了自动创建骨架

选择webapp,这样就会自动生成springmvc项目的一些基本文件。

2.配置java 文件 和资源文件

3.添加jar包**

springmvc项目需要两个依赖 一个是spring-webmvc 封装了基本的sping里面的web部分模块 有sping-context ,spring-aop等

org.springframework

spring-webhttp://mvc

5.2.5.RELEASE

javax.servlet

javax.servlet-api

3.1.0

4.springMVC项目web.xml初始化配置中央调度器以及自定义springmvc.xml文件

在tomcat启动的时候,一般会加载web.xml里面配置, 因为DispatcherServelt是调度器,负责调度用户的请求到相应的servlet处理,DispatcherServlet创建的时候,同时也会创建springmvc容器,那么tomcat启动之后,其他ervlet就可以使用了。

web.xml:

中央调度器dipathcerServlet配置创建 同时创建spingmvc配置spring监听器配置字符集过滤器配置

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

springDispatcherServlet

/

contextConfigLocation

classpath:spring.xml

org.springframework.web.context.ContextLoaderListener

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceRequestEncoding

true

forceResponseEncoding

true

encoding

/*

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

springDispatcherServlet

/

contextConfigLocation

classpath:spring.xml

org.springframework.web.context.ContextLoaderListener

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceRequestEncoding

true

forceResponseEncoding

true

encoding

/*

springmvc.xml:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd">


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java数据结构顺序表的详细讲解(数据结构定义一个顺序表)
下一篇:Spring如何按业务模块输出日志到不同的文件详解(springboot配置日志输出到文件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~