您的位置:首页 > 编程语言 > Java开发

springMVC 的工作原理和机制、配置

2017-06-05 00:00 501 查看

工作原理

下面的是springMVC的工作原理图:



1、客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web.xml中指定),web容器将请求转交给DispatcherServlet.

2、DipatcherServlet接收到这个请求之后将根据请求的信息(包括URL、Http方法、请求报文头和请求参数Cookie等)以及HandlerMapping的配置找到处理请求的处理器(Handler)。

3-4、DispatcherServlet根据HandlerMapping找到对应的Handler,将处理权交给Handler(Handler将具体的处理进行封装),再由具体的HandlerAdapter对Handler进行具体的调用。

5、Handler对数据处理完成以后将返回一个ModelAndView()对象给DispatcherServlet。

6、Handler返回的ModelAndView()只是一个逻辑视图并不是一个正式的视图,DispatcherSevlet通过ViewResolver将逻辑视图转化为真正的视图View。

7、Dispatcher通过model解析出ModelAndView()中的参数进行解析最终展现出完整的view并返回给客户端。

工作机制是什么

Control的调用(续)

接着对于(二)的补充:主要是小结下Control的处理逻辑的关键操作;

对于control的处理关键就是:DispatcherServlet的handlerMappings集合中根据请求的URL匹配每一个handlerMapping对象中的某个handler,匹配成功之后将会返回这个handler的处理连接handlerExecutionChain对象。而这个handlerExecutionChain对象中将会包含用户自定义的多个handlerInterceptor对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
*ReturntheHandlerExecutionChainforthisrequest.
*<p>Triesallhandlermappingsinorder.
*@paramrequestcurrentHTTPrequest
*@returntheHandlerExecutionChain,or<code>nullifnohandlercouldbefound
*/
protectedHandlerExecutionChaingetHandler(HttpServletRequestrequest)throwsException{
for(HandlerMappinghm:this.handlerMappings){
if(logger.isTraceEnabled()){
logger.trace(
"Testinghandlermap["+hm+"]inDispatcherServletwithname'"+getServletName()+"'");
}
HandlerExecutionChainhandler=hm.getHandler(request);
if(handler!=null){
returnhandler;
}
}
returnnull;
}
而对于handlerInterceptor接口中定义的三个方法中,preHandler和postHandler分别在handler的执行前和执行后执行,afterCompletion在view渲染完成、在DispatcherServlet返回之前执行。

愿意了解更多的技术知识分享可参考源码:http://minglisoft.cn/technology

朋友需要请加球球:2042849237

springmvc.xml的配置

视图解析器的配置:

<!--配置视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--使用前缀和后缀--><propertyname="prefix"value="/"></property><propertyname="suffix"value=".jsp"></property></bean>

在Controller中设置视图名的时候会自动加上前缀和后缀。

Controller的配置

自动扫描方式,扫描包下面所有的Controller,可以使用注解来指定访问路径。

<!--使用组件扫描的方式可以一次扫描多个Controller--><context:component-scanbase-package="com.wxisme.ssm.controller">
也可以使用单个的配置方式,需要指定Controller的全限定名。

<beanname="/queryUser.action"class="com.wxisme.ssm.controller.Controller1"/>
配置注解的处理器适配器和处理器映射器:

<!--注解的处理器适配器--><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!--注解的处理器映射器--><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
也可以使用下面的简化配置:

<!--配置注解的处理器映射器和处理器适配器--><mvc:annotation-drivenconversion-service="conversionService"></mvc:annotation-driven>
配置拦截器,可以直接定义拦截所有请求,也可以自定义拦截路径。

<mvc:interceptors><!--直接定义拦截所有请求--><beanclass="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!--<mvc:interceptor> 拦截所有路径的请求包括子路径 <mvc:mappingpath="/**"/> <beanclass="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean> </mvc:interceptor>--></mvc:interceptors>
配置全局异常处理器

<!--定义全局异常处理器--><!--只有一个全局异常处理器起作用--><beanid="exceptionResolver"class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>
配置文件上传数据解析器,在上传文件时需要配置。

<!--配置上传文件数据解析器--><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><propertyname="maxUploadSize"><value>9242880</value></property></bean>

还可以配置一些自定义的参数类型,以日期类型绑定为例。

<!--自定义参数类型绑定--><beanid="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><propertyname="converters"><list><!--日期类型绑定--><beanclass="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean>
上面提到过如果在配置前端控制器时拦截了所有的请求,不做特殊处理就会导致部分静态资源无法使用。如果是这种情况就可以使用下面的配置来访问静态资源文件。

<mvc:resourcesmapping="/images/**"location="/images/"/><mvc:resourcesmapping="/css/**"location="/css/"/><mvc:resourcesmapping="/js/**"location="/js/"/><mvc:resourcesmapping="/imgdata/**"location="/imgdata/"/>
也可以使用默认,但是需要在web.xml中配置。

<!--访问静态资源文件--><!--<mvc:default-servlet-handler/>需要在web.xml中配置-->
完全可以不拦截所有路径,大可避免这个问题的发生。

完整的配置大概是这样的,需要注意xml文件的命名空间,有时候会有影响的。

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--使用前缀和后缀--><propertyname="prefix"value="/"></property><propertyname="suffix"value=".jsp"></property></bean><!--使用组件扫描的方式可以一次扫描多个Controller--><context:component-scanbase-package="com.wxisme.ssm.controller"></context:component-scan><!--配置注解的处理器映射器和处理器适配器--><mvc:annotation-drivenconversion-service="conversionService"></mvc:annotation-driven><!--自定义参数类型绑定--><beanid="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><propertyname="converters"><list><!--日期类型绑定--><beanclass="com.wxisme.ssm.controller.converter.DateConverter"/></list></property></bean><!--访问静态资源文件--><!--<mvc:default-servlet-handler/>需要在web.xml中配置--><mvc:resourcesmapping="/images/**"location="/images/"/><mvc:resourcesmapping="/css/**"location="/css/"/><mvc:resourcesmapping="/js/**"location="/js/"/><mvc:resourcesmapping="/imgdata/**"location="/imgdata/"/><!--定义拦截器--><mvc:interceptors><!--直接定义拦截所有请求--><beanclass="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean><!--<mvc:interceptor> 拦截所有路径的请求包括子路径 <mvc:mappingpath="/**"/> <beanclass="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean> </mvc:interceptor>--></mvc:interceptors><!--配置上传文件数据解析器--><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><propertyname="maxUploadSize"><value>9242880</value></property></bean><!--定义全局异常处理器--><!--只有一个全局异常处理器起作用--><beanid="exceptionResolver"class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean></beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息