springMVC 的工做原理和機制、配置

工做原理

下面的是springMVC的工做原理圖:
css

一、客戶端發出一個http請求給web服務器,web服務器對http請求進行解析,若是匹配DispatcherServlet的請求映射路徑(在web.xml中指定),web容器將請求轉交給DispatcherServlet.前端

二、DipatcherServlet接收到這個請求以後將根據請求的信息(包括URL、Http方法、請求報文頭和請求參數Cookie等)以及HandlerMapping的配置找處處理請求的處理器(Handler)。web

3-四、DispatcherServlet根據HandlerMapping找到對應的Handler,將處理權交給Handler(Handler將具體的處理進行封裝),再由具體的HandlerAdapter對Handler進行具體的調用。spring

五、Handler對數據處理完成之後將返回一個ModelAndView()對象給DispatcherServlet。spring-mvc

六、Handler返回的ModelAndView()只是一個邏輯視圖並非一個正式的視圖,DispatcherSevlet經過ViewResolver將邏輯視圖轉化爲真正的視圖View。服務器

七、Dispatcher經過model解析出ModelAndView()中的參數進行解析最終展示出完整的view並返回給客戶端。
 mvc

工做機制是什麼app

Control的調用(續)

接着對於(二)的補充:主要是小結下Control的處理邏輯的關鍵操做;jsp

對於control的處理關鍵就是:DispatcherServlet的handlerMappings集合中根據請求的URL匹配每個handlerMapping對象中的某個handler,匹配成功以後將會返回這個handler的處理鏈接handlerExecutionChain對象。而這個handlerExecutionChain對象中將會包含用戶自定義的多個handlerInterceptor對象。post

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

     * Return the HandlerExecutionChain for this request.

     * <p>Tries all handler mappings in order.

     * @param request current HTTP request

     * @return the HandlerExecutionChain, or <code>null if no handler could be found

     */

    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {

        for (HandlerMapping hm : this.handlerMappings) {

            if (logger.isTraceEnabled()) {

                logger.trace(

                        "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");

            }

            HandlerExecutionChain handler = hm.getHandler(request);

            if (handler != null) {

                return handler;

            }

        }

        return null;

    }

而對於handlerInterceptor接口中定義的三個方法中,preHandler和postHandler分別在handler的執行前和執行後執行,afterCompletion在view渲染完成、在DispatcherServlet返回以前執行。
 

願意瞭解更多的技術知識分享可參考源碼:http://minglisoft.cn/technology

朋友須要請加球球:2042849237

springmvc.xml的配置                                                                                                                                                

視圖解析器的配置:

<!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用前綴和後綴 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>

 

在Controller中設置視圖名的時候會自動加上前綴和後綴。

Controller的配置

自動掃描方式,掃描包下面全部的Controller,可使用註解來指定訪問路徑。

<!-- 使用組件掃描的方式能夠一次掃描多個Controller --> <context:component-scan base-package="com.wxisme.ssm.controller">

也可使用單個的配置方式,須要指定Controller的全限定名。

<bean name="/queryUser.action" class="com.wxisme.ssm.controller.Controller1"/>

配置註解的處理器適配器和處理器映射器:

<!-- 註解的處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 註解的處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

也可使用下面的簡化配置:

<!-- 配置註解的處理器映射器和處理器適配器 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

配置攔截器,能夠直接定義攔截全部請求,也能夠自定義攔截路徑。

<mvc:interceptors> <!-- 直接定義攔截全部請求 --> <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean> <!-- <mvc:interceptor>
            攔截全部路徑的請求   包括子路徑
            <mvc:mapping path="/**"/>
            <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>
        </mvc:interceptor> --> </mvc:interceptors>

 配置全局異常處理器

<!-- 定義全局異常處理器 --> <!-- 只有一個全局異常處理器起做用 --> <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>

配置文件上傳數據解析器,在上傳文件時須要配置。

<!--配置上傳文件數據解析器  --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>9242880</value> </property> </bean>

 

還能夠配置一些自定義的參數類型,以日期類型綁定爲例。

<!-- 自定義參數類型綁定 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <!-- 日期類型綁定 --> <bean class="com.wxisme.ssm.controller.converter.DateConverter"/> </list> </property> </bean>

上面提到過若是在配置前端控制器時攔截了全部的請求,不作特殊處理就會致使部分靜態資源沒法使用。若是是這種狀況就可使用下面的配置來訪問靜態資源文件。

<mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/imgdata/**" location="/imgdata/" />

也可使用默認,可是須要在web.xml中配置。

<!-- 訪問靜態資源文件 --> <!-- <mvc:default-servlet-handler/> 須要在web.xml中配置-->

徹底能夠不攔截全部路徑,大可避免這個問題的發生。

完整的配置大概是這樣的,須要注意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" 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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用前綴和後綴 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 使用組件掃描的方式能夠一次掃描多個Controller --> <context:component-scan base-package="com.wxisme.ssm.controller"> </context:component-scan> <!-- 配置註解的處理器映射器和處理器適配器 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 自定義參數類型綁定 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <!-- 日期類型綁定 --> <bean class="com.wxisme.ssm.controller.converter.DateConverter"/> </list> </property> </bean> <!-- 訪問靜態資源文件 --> <!-- <mvc:default-servlet-handler/> 須要在web.xml中配置--> <mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/imgdata/**" location="/imgdata/" /> <!-- 定義攔截器 --> <mvc:interceptors> <!-- 直接定義攔截全部請求 --> <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean> <!-- <mvc:interceptor>
            攔截全部路徑的請求   包括子路徑
            <mvc:mapping path="/**"/>
            <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>
        </mvc:interceptor> --> </mvc:interceptors> <!--配置上傳文件數據解析器  --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>9242880</value> </property> </bean> <!-- 定義全局異常處理器 --> <!-- 只有一個全局異常處理器起做用 --> <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean> </beans>
相關文章
相關標籤/搜索