springmvc配置文件详解

1. context:component-scan参考地址:http://blog.csdn.net/menghuanzhiming/article/details/77979986
2. context:component-scan替代context:annotation-config,参考地址:http://mushiqianmeng.blog.51cto.com/3970029/723880/
3. 处理器映射器、处理器适配器、异常处理和mvc:annotation-driven标签的关系,mvc:annotation-driven标签默认的注册了映射器、适配器、异常处理器,最好不使用mvc:annotation-driven,而是显示的手动配置,
参考地址:http://www.cnblogs.com/yangzhilong/p/3725849.html
(1)映射器、适配器、异常处理等都可以配置多个但是匹配时都是由上到下依次匹配执行;
(2)同一个映射器在配置文件中配置了两次,springmvc会使用第一次生成的映射器;
(3)映射器会自动调用与之匹配成对的适配器;
(4)不同的适配器向拦截器传的参数不同,拦截器获取方法的方式不同,参考地址:http://chenzhou123520.iteye.com/blog/1702563,建议不要使用AnnotationMethodHandlerAdapter适配器,使用RequestMappingHandlerAdapter;
(5)适配器中配置的属性messageConverters,接收参数和返回参数,可以通过配置编码解决中文乱码问题,参考地址:http://blog.csdn.net/a67474506/article/details/46364159
(6)springmvc全局数据校验和局部数据校验的配置方式:
注意:本质其实就是将校验器绑定到处理器适配器上;
参考地址:http://blog.csdn.net/menghuanzhiming/article/details/78059876

4. 视图解析器;

5. 配置静态资源springmvc不进行拦截,代码如下:
只有当springmvc配置完全拦截时才需要配置,静态资源不拦截;

方式1:

<!--location:文件实际路径。 mapping:请求的URL格式 -->
<mvc:resources location="/WEB-INF/js/" mapping="/js/**" />
<mvc:resources location="/WEB-INF/css/" mapping="/css/**" />

方式2:使用默认的servlet处理,
参考地址:http://www.cnblogs.com/fangqi/archive/2012/10/28/2743108.html

<mvc:default-servlet-handler />

6. 定时任务注解task:annotation-driven的使用,参考地址:
http://blog.csdn.net/medivhq/article/details/50945311

<!-- task定时任务扫描注解@Scheduled -->
<task:annotation-driven/>

配置如下:

<?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" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd 
                            http://www.springframework.org/schema/task
                            http://www.springframework.org/schema/task/spring-task-3.1.xsd   
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置一个编码bean,用于统一字符集 -->
    <bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName">
        <constructor-arg value="UTF-8"/>
    </bean>

    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="edu.hrbeu.platform.modeling" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

    <!-- 处理器映射器,启动项目时开始进行映射  -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!-- 处理器适配器,执行拦截器和后端控制器,并配置相应的数据转换类和数据校验类 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!--类型转换  -->
        <property name="messageConverters">  
            <list>  
                <ref bean="string_hmc" /> 
                <ref bean="byteArray_hmc" />  
                <ref bean="resource_hmc" />  
                <ref bean="source_hmc" />  
                <ref bean="xmlAwareForm_hmc" />  
                <ref bean="jaxb2RootElement_hmc" />  
                <ref bean="jackson_hmc" />  
            </list>
        </property>
        <!-- 数据校验 -->
        <property name="webBindingInitializer" ref="webBindingInitializer"></property>
    </bean>
    <!-- 数据转换 -->
    <bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter"><!-- 处理字符串.. -->
        <constructor-arg ref="utf8Charset"/>
    </bean>
    <bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 处理.. -->
    <bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 处理.. -->
    <bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 处理.. -->
    <bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 处理.. -->
    <bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 处理.. -->
    <bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 处理json-->

    <!-- 全局数据校验 -->
    <!-- 数据验证 Validator 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="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  
         <property name="validator" ref="validator"/>  
    </bean>

    <!-- 全局异常配置 -->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">errors/error</prop>
            </props>
        </property>
        <property name="statusCodes">
            <props>
                <prop key="errors/error">500</prop>
                <prop key="errors/404">404</prop>
            </props>
        </property>
        <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->
        <property name="warnLogCategory" value="WARN"></property>
        <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
        <property name="defaultErrorView" value="errors/error"></property>
        <!-- 默认HTTP状态码 -->
        <property name="defaultStatusCode" value="500"></property>
    </bean>

    <!-- <mvc:annotation-driven validator="validator">
        <mvc:message-converters>
             配置responsebody时返回为简单字符串乱码问题 
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg ref="utf8Charset"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven> -->

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 配置静态资源springmvc不拦截 -->
    <mvc:resources location="/image/" mapping="/image/**"></mvc:resources>

    <!-- 配置文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>

    <!-- 装配拦截器 -->
    <mvc:interceptors>
        <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->  
        <!-- <bean class="edu.hrbeu.platform.modeling.common.interceptor.LogInterceptor"/>   -->
        <mvc:interceptor>
            <!-- 在springmvc前端控制器DispatcherServlet的基础上缩小拦截范围 -->
            <!-- 一级目录 -->
            <mvc:mapping path="/*.do" />
            <!-- 二级目录 -->
            <!-- <mvc:mapping path="/*/*.do" /> -->
            <!-- 不知道和 /*/*.do有什么区别-->
            <mvc:mapping path="/**/*.do" />
            <!--错误配置样例  -->
            <!-- 
                拦截失败
                <mvc:mapping path="*.do" />
                <mvc:mapping path="/**.do" />
             -->
            <!-- 需排除拦截的地址 -->    
           <!--  <mvc:exclude-mapping path="/" />  
            <mvc:exclude-mapping path="/test" />   -->
            <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->  
            <bean class="edu.hrbeu.platform.modeling.common.interceptor.LogInterceptor"/>  
        </mvc:interceptor>  
    </mvc:interceptors>

</beans>  
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值