springmvc三大組件(二)

springmvc的一個特色:本質是控制器。java

1.HandlerMapping:根據請求的url找到 處理器的方法。web

maven dependencies/spring-webmvc/../DispatcherServlet.properties默認的處理器映射器已過期,項目中不推薦使用。spring

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

配置:resources/springmvc.xml瀏覽器

選中「org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping」ctrl+shift+t快速進入對應類,讀目標類的對應註解和註釋。spring-mvc

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置包掃描controller -->
        <context:component-scan base-package="com.buff.controller"></context:component-scan>
        
        <!-- 配置處理器映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

</beans>

 

2.HandlerAdapter:執行處理器方法。tomcat

默認的處理器適配器已過期,項目中不推薦使用。mvc

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
<!-- 配置處理器適配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

映射器和適配器配套使用,要麼都不配置,不然http status 500.app

3.註解驅動方式配置 處理器映射器 和 處理器適配器jsp

<!-- 說明:
        	1.等於同時配置了RequestMappingHandlerMapping/RequestMappingHandlerAdapter
        	2.應用項目中的使用方式
         -->
        <mvc:annotation-driven></mvc:annotation-driven>

4.ViewResolver:把邏輯視圖(在處理器方法中設置的視圖路徑)解析成物理視圖(在瀏覽器實際看到的頁面)。maven

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
<!-- 配置視圖解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<!-- 配置視圖的公共的目錄路徑 -->
        	<property name="prefix" value="/WEB-INF/jsp/"></property>
        	
        	<!-- 配置視圖的擴展名稱 -->
        	<property name="suffix" value=".jsp"></property>
        
        </bean>

*公共資源可抽取出進行配置。

*更改代碼後,從新啓動tomcat前要點擊console->terminate

相關文章
相關標籤/搜索