springMVC --配置詳細與註解說明

<?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:tx="http://www.springframework.org/schema/tx"
	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-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- 自動掃描的包名 -->
    <context:component-scan base-package="com.app,com.core,JUnit4" ></context:component-scan>
    
    <!-- 默認的註解映射的支持 -->
    <mvc:annotation-driven />
    
    <!-- 視圖解釋類 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/><!--可爲空,方便實現自已的依據擴展名來選擇視圖解釋類的邏輯  -->
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    </bean>
    
	<!-- 攔截器 -->
    <mvc:interceptors>
		<bean class="com.core.mvc.MyInteceptor" />
	</mvc:interceptors>	  
	
 	<!-- 對靜態資源文件的訪問  方案一 (二選一) -->
 	<mvc:default-servlet-handler/>
 	
 	<!-- 對靜態資源文件的訪問  方案二 (二選一)-->
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

</beans>


<context:component-scan/> 掃描指定的包中的類上的註解,經常使用的註解有:css

@Controller 聲明Action組件
@Service    聲明Service組件    @Service("myMovieLister") 
@Repository 聲明Dao組件
@Component   泛指組件, 當很差歸類時. 
@RequestMapping("/menu")  請求映射
@Resource  用於注入,( j2ee提供的 ) 默認按名稱裝配,@Resource(name="beanName") 
@Autowired 用於注入,(srping提供的) 默認按類型裝配 
@Transactional( rollbackFor={Exception.class}) 事務管理
@ResponseBody 該註解用於讀取Request請求的body部分數據,使用系統默認配置的HttpMessageConverter進行解析,而後把相應的數據綁定到要返回的對象上;
@Scope("prototype")   設定bean的做用域html

 

<mvc:annotation-driven /> 是一種簡寫形式,徹底能夠手動配置替代這種簡寫形式,簡寫形式能夠讓初學都快速應用默認配置方案。<mvc:annotation-driven /> 會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,是spring MVC爲@Controllers分發請求所必須的。
並提供了:數據綁定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB),讀寫JSON的支。
咱們處理響應ajax請求時,就使用到了對json的支持。
對action寫JUnit單元測試時,要從spring IOC容器中取DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,來完成測試,取的時候要知道是<mvc:annotation-driven />這一句註冊的這兩個bean。web


<mvc:interceptors/> 是一種簡寫形式。經過看前面的大圖,知道,咱們能夠配置多個HandlerMapping。<mvc:interceptors/>會爲每個HandlerMapping,注入一個攔截器。其實咱們也能夠手動配置爲每一個HandlerMapping注入一個攔截器。ajax

 

<mvc:default-servlet-handler/> 使用默認的Servlet來響應靜態文件。spring

 

<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> 匹配URL  /images/**  的URL被當作靜態資源,由Spring讀出到內存中再響應httpjson


springMVC --DispatcherServlet詳解
spring-mvc

相關文章
相關標籤/搜索