1. 程式人生 > >SpringMVC 處理器對映器、處理器介面卡

SpringMVC 處理器對映器、處理器介面卡

一、非註解處理器對映器和介面卡
1.非註解的處理器對映器
1)org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
2)org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
SimpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增強版本,它可以將url和處理器bean的id進行統一對映配置。

多個對映器可以並存,前端控制器判斷url能讓哪個對映器對映,就讓正確的對映器處理。


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 對itemsController1進行url對映,url是/queryItems1.action -->
<prop key="/queryItems1.action">itemController1<prop/>
<prop key="/queryItems2.action">itemController1<prop/>
</props>
</property>
</bean>


2.非註解的處理器介面卡

1)org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter:要求編寫的Handler實現Controller介面
2)org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter:要求編寫的Handler實現HttpRequestHandler介面。
二、註解的處理器對映器和介面卡
1)介紹
spring3.1之前
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
註解對映器
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter註解介面卡
spring3.1之後
在spring3.1之後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter註解介面卡。
在spring3.1之後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
註解對映器。

2)配置
a)方式一(不常用)


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

註解的對映器和註解的介面卡必須配對使用。
b)方式二(常用)


<mvc:annotation-driven></mvc:annotation-driven>
mvc:annotation-driven預設載入很多引數繫結方法,比如json轉換就預設載入了。

三、開發註解Handler

1)@RequestMapping("/xxx")
2)spring容器中載入Handler。元件掃描的方式:


<context:component-scan base-package="cn.itcast.ssm.controller"/>
四、配置檢視解析器的字首和字尾


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