spring MVC 配置文件解析

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<span>  <span class="comments"><!-- 默认的注解映射的支持 --></span><span>  </span></span>
    <mvc:annotation-driven />
 


    <util:properties id="params" location="WEB-INF/properties/parameters.properties"/>
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<span> <span class="comments"><!-- 自动扫描的包名 可以扫描多个包 逗号分隔  --></span><span></span></span>
-> <context:component-scan base-package="com.abc"/>
    <context:annotation-config/>
  <!-- 对静态资源文件的访问  方案一 (二选一) -->    
<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"/> 
      <mvc:resources mapping="/style/**" location="/style/"/>

<!-- 视图解释类 --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1"/> <property name="ignoreAcceptHeader" value="true"/> <property name="defaultContentType" value="text/html"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  --> 
</bean> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> </bean>  <!-- 拦截器 -->  
  <mvc:interceptors>      
  <bean class="com.core.mvc.MyInteceptor" />  
  </mvc:interceptors> 
<span style="font-size:12px;"> <!-如果当前路径是/ 则重定向到/admin/index-></span>   
<mvc:view-controller path="/" view-name="redirect:/admin/index"/>
 <!- 如果当前路径是/ 则交给相应的视图解析器直接解析为视图-> <mvc:view-controller path="/" view-name="hello" /> <mvc:default-servlet-handler/><!- 自动注册  的SimpleUrlHandlerMapping  的order属性值是:  2147483647-></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。

===============================================================================================================================

<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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-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
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list >
                <ref bean="mappingJacksonHttpMessageConverter" />
                <!--  本地化UTF-8编码StringHttpMessageConverter  -->
                <bean id="stringHttpMessageConverter" class="com.pay.risk.util.Utf8StringHttpMessageConverter">  
                     <property name = "supportedMediaTypes">
                        <list>
                            <value>application/json; charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>plain/text; charset=UTF-8</value>
                        </list>
                    </property>  
                </bean>
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    
    <mvc:annotation-driven />

    <!-- 配置支持标注 -->
    <context:annotation-config />
    
    <!-- 自动扫描bean,把作了注解的类转换为bean -->

    <context:component-scan base-package="com.pay.risk.action" />
    <context:component-scan base-package="com.pay.risk.taglib" />
    
    
    <!--  Resource filter  -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/image/**" location="/image/" />
    <mvc:resources mapping="/report/**" location="/report/" />
    <mvc:resources mapping="/templates/**" location="/templates/" />
    
    <!-- 注册权限拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.pay.risk.interceptor.LoginPermissionInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <!-- springmvc传json值时的乱码解决
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name = "supportedMediaTypes">
            <list>
                <value>application/json; charset=UTF-8</value>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>   
    </bean>
    -->
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <!--
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />-->
    
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">  
       <property name="basename" value="viewBean"/>  
       <property name="order" value="1"/>  
    </bean>

<!--     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" -->
<!--         p:defaultEncoding="utf-8" />   -->
    
     <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8">  
        <!-- 指定所上传文件的总大小不能超过2M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
        <property name="maxUploadSize" value="2000000"/>  
    </bean>  
      
    <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->  
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/jsp/error_fileupload.jsp</prop>  
            </props>  
        </property>  
    </bean>
     
</beans>

==============================================================================================================================

<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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-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
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list >
                <ref bean="mappingJacksonHttpMessageConverter" />
                <!--  本地化UTF-8编码StringHttpMessageConverter  -->
                <bean id="stringHttpMessageConverter" class="com.pay.risk.util.Utf8StringHttpMessageConverter">  
                     <property name = "supportedMediaTypes">
                        <list>
                            <value>application/json; charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>plain/text; charset=UTF-8</value>
                        </list>
                    </property>  
                </bean>
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    
    <mvc:annotation-driven />

    <!-- 配置支持标注 -->
    <context:annotation-config />
    
    <!-- 自动扫描bean,把作了注解的类转换为bean -->

    <context:component-scan base-package="com.pay.risk.action" />
    <context:component-scan base-package="com.pay.risk.taglib" />
    
    
    <!--  Resource filter  -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/image/**" location="/image/" />
    <mvc:resources mapping="/report/**" location="/report/" />
    <mvc:resources mapping="/templates/**" location="/templates/" />
    
    <!-- 注册权限拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.pay.risk.interceptor.LoginPermissionInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <!-- springmvc传json值时的乱码解决
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name = "supportedMediaTypes">
            <list>
                <value>application/json; charset=UTF-8</value>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>   
    </bean>
    -->
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <!--
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />-->
    
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">  
       <property name="basename" value="viewBean"/>  
       <property name="order" value="1"/>  
    </bean>

<!--     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" -->
<!--         p:defaultEncoding="utf-8" />   -->
    
     <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8">  
        <!-- 指定所上传文件的总大小不能超过2M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
        <property name="maxUploadSize" value="2000000"/>  
    </bean>  
      
    <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->  
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/jsp/error_fileupload.jsp</prop>  
            </props>  
        </property>  
    </bean>
     
</beans>



















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值