在 Spring MVC 中
org.springframework.web.servlet.DispatcherServlet 是核心Servlet。
一个MVC应用可以有多个
DispatcherServlet 。每个有自己的WebApplicationContext配置。Spring MVC还提供一个Root级别的WebApplicationContext。
DispatcherServlet主要用于控制流程,主要职责如下:
1、整个过程开始于Web服务器接收到一个请求,如果匹配
DispatcherServlet 的请求映射路径(web.xml),Web容器将请求转交给
DispatcherServlet 处理 ;
2、DispatcherServlet 接收到一个请求后,将根据请求的信息(URL,请求参数等)及HandlerMapping 的配置找到处理请求的处理器(Hanlder);
3、当DispatcherServlet找到对应的Handler后,通过HandlerAdapter对Handler进行封装,再以统一的适配器接口调用Handler。
4、处理器完成处理后将返回一个ModelAndView ,包含了视图逻辑名和模型数据;
5、DispatcherServlet 通过ViewResolver完成逻辑视图名到真实视图对象的解析;
6、最终DispatcherServlet使用真实View对象与ModelAndView中数据进行渲染具体的视图等;
DispatcherServlet 配置
web.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"3.0"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:spring/application-config.xml
</
param-value
>
</
context-param
>
<!-- Spring MVC filters -->
<
filter
>
<
filter-name
>
CharacterEncodingFilter
</
filter-name
>
<
filter-class
>
org.springframework.web.filter.CharacterEncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
UTF-8
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
forceEncoding
</
param-name
>
<
param-value
>
true
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
CharacterEncodingFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!-- Processes application requests -->
<
servlet
>
<
servlet-name
>
appServlet
</
servlet-name
>
<
servlet-class
>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/ mvc-config.xml
</
param-value
>
</
init-param
>
<
load-on-startup
>
1
</
load-on-startup
>
<
multipart-config
>
<
max-file-size
>
5000000
</
max-file-size
>
</
multipart-config
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
appServlet
</
servlet-name
>
<
url-pattern
>
/
</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
DispatcherServlet ( mvc -config.xml)
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation
=
"http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"
>
<context:component-scan base-package= "com.yln.spring.web" />
<mvc:annotation-driven />
<bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<
property
name
=
"prefix"
value
=
"/WEB-INF/view/"
/>
<
property
name
=
"suffix"
value
=
".jsp"
/>
</
bean
>
</
beans
>
Spring MVC的配置非常简单,是因为框架在内部做了很多默认设置。在MVC的Jar包中有一个 DispatcherServlet.properties文件,定义了默认装载的类。这里定义的bean都可以在XML中配置,以达到定制的效果。MVC的学习实际也就是学习下面各个Bean的用法。
org.springframework.web.servlet.LocaleResolver=
org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=
org.springframework.web.servlet.theme.FixedThemeResolver
org.springframework.web.servlet.HandlerMapping=
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
org.springframework.web.servlet.HandlerAdapter=
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=
org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=
org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=
org.springframework.web.servlet.support.SessionFlashMapManager
代码下载: 代码下载