Spring MVC学习-----------springMVC-mvc.xml

springMVC-mvc.xml 配置文件片段解说 (未使用默认配置文件名称)

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"    
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  10.     http://www.springframework.org/schema/tx   
  11.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.     http://www.springframework.org/schema/context  
  13.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  14.     http://www.springframework.org/schema/mvc  
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  16.   
  17.   
  18.     <!-- 自己主动扫描的包名 -->  
  19.     <context:component-scan base-package="com.app,com.core,JUnit4" ></context:component-scan>  
  20.       
  21.     <!-- 默认的注解映射的支持 -->  
  22.     <mvc:annotation-driven />  
  23.       
  24.     <!-- 视图解释类 -->  
  25.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  26.         <property name="prefix" value="/WEB-INF/jsp/"/>  
  27.         <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的根据扩展名来选择视图解释类的逻辑  -->  
  28.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  29.     </bean>  
  30.       
  31.     <!-- 拦截器 -->  
  32.     <mvc:interceptors>  
  33.         <bean class="com.core.mvc.MyInteceptor" />  
  34.     </mvc:interceptors>       
  35.       
  36.     <!-- 对静态资源文件的訪问  方案一 (二选一) -->  
  37.     <mvc:default-servlet-handler/>  
  38.       
  39.     <!-- 对静态资源文件的訪问  方案二 (二选一)-->  
  40.     <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
  41.     <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>  
  42.     <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>  
  43.   
  44. </beans>   

 

<context:component-scan/> 扫描指定的包中的类上的注解,经常使用的注解有:

@Controller 声明Action组件
@Service    声明Service组件    @Service("myMovieLister") 
@Repository 声明Dao组件
@Component   泛指组件, 当不好归类时. 
@RequestMapping("/menu")  请求映射
@Resource  用于注入。( j2ee提供的 ) 默认按名称装配。@Resource(name="beanName") 
@Autowired 用于注入,(srping提供的) 默认按类型装配 
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody
@Scope("prototype")   设定bean的作用域

 

<mvc:annotation-driven /> 是一种简写形式,全然能够手动配置替代这样的简写形式,简写形式能够让初学都高速应用默认配置方案。<mvc:annotation-driven /> 会自己主动注冊DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。


并提供了:数据绑定支持。@NumberFormatannotation支持。@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面。对action写JUnit单元測试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完毕測试,取的时候要知道是<mvc:annotation-driven />这一句注冊的这两个bean。

怎样替换 <mvc:annotation-driven />?他究竟做了什么工作,请看。最后面的 十九节 <mvc:annotation-driven /> 究竟做了什么工作。

 

<mvc:interceptors/> 是一种简写形式。通过看前面的大图。知道,我们能够配置多个HandlerMapping。

<mvc:interceptors/>会为每个HandlerMapping,注入一个拦截器。事实上我们也能够手动配置为每个HandlerMapping注入一个拦截器。

 

<mvc:default-servlet-handler/> 使用默认的Servlet来响应静态文件。

 

<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> 匹配URL  /images/**  的URL被当做静态资源,由Spring读出到内存中再响应http。

转载请注明出处:本文地址:http://elf8848.iteye.com/blog/875830

原文地址:https://www.cnblogs.com/wzzkaifa/p/6769309.html