springmvc xml 常用配置方式(拦截器,文件上传,适配器等)

来源:互联网 发布:mac系统如何关闭程序 编辑:程序博客网 时间:2024/06/06 08:46
<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">    <!-- 可以扫描controller、service、...    这里让扫描controller,指定controller的包     -->    <context:component-scan base-package="cn.ssm.controller"></context:component-scan>        <!-- 静态资源解析    包括 :js、css、img、..     -->     <mvc:resources location="/js/" mapping="/js/**"/>     <mvc:resources location="/img/" mapping="/img/**"/>                <!--注解映射器 -->    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->    <!--注解适配器 -->    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->        <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置    mvc:annotation-driven默认加载很多的参数绑定方法,    比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter    实际开发时使用mvc:annotation-driven     -->    <mvc:annotation-driven conversion-service="conversionService"    validator="validator"></mvc:annotation-driven>        <!-- 视图解析器    解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包     -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 配置jsp路径的前缀 -->        <property name="prefix" value="/WEB-INF/jsp/"/>        <!-- 配置jsp路径的后缀 -->        <property name="suffix" value=".jsp"/>    </bean>        <!-- 自定义参数绑定 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!-- 转换器 -->        <property name="converters">            <list>                <!-- 日期类型转换 -->                <bean class="cn.ssm.controller.converter.CustomDateConverter"/>            </list>        </property>                </bean>        <!-- 校验器 -->    <bean id="validator"        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <!-- hibernate校验器-->        <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />        <!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties -->        <property name="validationMessageSource" ref="messageSource" />    </bean><!-- 校验错误信息配置文件 -->    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <!-- 资源文件名-->        <property name="basenames">               <list>                <value>classpath:CustomValidationMessages</value>            </list>           </property>        <!-- 资源文件编码格式 -->        <property name="fileEncodings" value="utf-8" />        <!-- 对资源文件内容缓存时间,单位秒 -->        <property name="cacheSeconds" value="120" />    </bean>    <!-- 全局异常处理器    只要实现HandlerExceptionResolver接口就是全局异常处理器     -->    <bean class="cn.ssm.exception.CustomExceptionResolver"></bean>            <!-- 文件上传 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <!-- 设定默认编码 -->        <property name="defaultEncoding" value="UTF-8"></property>        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->        <property name="maxUploadSize" value="5242880"></property>    </bean>        <!--拦截器 --><mvc:interceptors>    <!--多个拦截器,顺序执行 -->    <!-- 登陆认证拦截器 -->    <mvc:interceptor>        <mvc:mapping path="/**"/>        <bean class="cn.ssm.interceptor.LoginInterceptor"></bean>    </mvc:interceptor>    <mvc:interceptor>        <!-- /**表示所有url包括子url路径 -->        <mvc:mapping path="/**"/>        <bean class="cn.ssm.interceptor.HandlerInterceptor1"></bean>    </mvc:interceptor>    <mvc:interceptor>        <mvc:mapping path="/**"/>        <bean class="cn.ssm.interceptor.HandlerInterceptor2"></bean>    </mvc:interceptor></mvc:interceptors>            </beans>


0 0
原创粉丝点击