1. 程式人生 > >SpringMVC第四篇——處理器對映器,處理器介面卡,檢視解析器的配置

SpringMVC第四篇——處理器對映器,處理器介面卡,檢視解析器的配置

這兩個註解的作用和來源:

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""/>
            <property name="suffix" value=""/>
        </bean>

1:預設載入的對映器處理器和對映器配置器

入門程式只是配置了元件掃描器
使用`<context
:component-scan>`自動掃描標記@Controller的控制器類, 並沒有配置處理器對映器和處理器介面卡;

根據SpringMVC的架構和執行流程,是前端控制器呼叫處理器對映器,呼叫處理器介面卡,執行處理器。是因為SpringMVC中在載入完前端控制器之後,會預設的加處理器對映器和處理器介面卡;

SpringMVC載入.DispatcherServlet.properties配置檔案:

這裡寫圖片描述

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping


對映器:預設載入兩個處理器對映器。

處理器對映器

使用配置方式開發:(需要實現implements Controller)

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.mvc.annotation.AnnotationMethodHandlerAdapter

如果controller的實現使用的是:

public class Show  implements HttpRequestHandler{},

不使用@controller的註解,那麼就會載入

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\ ,這個處理器介面卡:

如果使用的是:

public class Show  implements Controller{}
就會載入:  org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\

我們使用的是:@controller,所以載入的是:

 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

2:關聯原始碼,檢視載入的類,

開啟註解方式的預設的對映器處理器原始碼:

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

關聯原始碼包:
spring-webmvc-4.1.3.RELEASE-sources.jar

發現:
這裡寫圖片描述

*預設的處理器對映器: `DefaultAnnotationHandlerMapping已經過期了,但是還是能夠使用:
在spring3.2和之後的之後的版本,使用的是

**org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping**

同樣開啟介面卡原始碼:預設的處理器介面卡:AnnotationMethodHandlerAdapter
所以當我們使用controller的註解類,使用requestMapping的註解方法。那麼專案執行起來,前端控制器載入之後,就會預設的載入了處理器對映器org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
和處理器介面卡。org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

2:如果我們要使用新的處理器對映器:
那我們就不使用預設的,我們自己進行配置:

 <!-- 處理器對映器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
        <!-- 處理器介面卡 -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

這樣就前端控制器載入之後,就會載入我們配置的處理器對映器和處理器介面卡。

這樣使用比較麻煩:使用註解驅動代替上面的配置

 <mvc:annotation-driven/>

自動載入RequestMappingHandlerMapping和RequestMappingHandlerAdapter這兩個類

3:檢視解析器的配置

  <!-- 檢視直譯器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"/><!-- 字首 -->
            <property name="suffix" value=".jsp"/><!-- 字尾 -->
        </bean>

就是會在返回的所有的檢視路徑上自動加上字首和字尾
入門程式中:返回的檢視路徑:

ma.setViewName("/WEB-INF/hello.jsp");

配置好了檢視解析器之後因為會自動加上字首和字尾。只需要寫成:

ma.setViewName("hello");

4.總結:

1:使用註解方式進行SpringMVC進行開發的時候,不配處理器對映器和處理器介面卡,框架會預設載入DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter。
2:從spring3.1之後這兩個類就過期了,但是還能使用。我們開發時候使用新的類。需要在配置檔案中進行配置。我們使用註解驅動:

 <mvc:annotation-driven/>

這個配置是替代了配置:

 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

3:檢視解析器配置:

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""/><!-- 字首 -->
            <property name="suffix" value=""/><!-- 字尾 -->
        </bean>

檢視解析器,可以不用配置,是為了方便開發使用,

返回 SpringMVC的學習筆記目錄