springmvc.xml常用配置

spring-mvc.xml配置文件内容如下:

<?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: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.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	   
    <!--
        注册HandlerMapping(处理器映射器)和HandlerAdapter(处理器适配器)的实现类;
        如果前端控制器url-pattern设置的是/,不加会导致无法区分请求是资源文件还是mvc的注解,导致报404
    -->
    <mvc:annotation-driven >
        <!-- 定义消息转换器,支持springMVC返回JSON格式 -->
        <mvc:message-converters>
            <bean id="fastJson" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
  
    <!-- Controller层次扫描 -->
    <context:component-scan base-package="controller">
        <!-- context:include-filter,负责扫描Controller层注解@Controller -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <!-- 不扫描@Service注解,防止Spring重复创建同一个类的实例,避免父子容器产生冲突 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>      
   
    <!-- 配置静态资源映射 -->
    <mvc:resources  mapping="/css/**"   location="/resources/css/" />
    <mvc:resources  mapping="/js/**"    location="/resources/js/" />
    <mvc:resources  mapping="/html/**"    location="/resources/html/" />
    <mvc:resources  mapping="/images/**"    location="/resources/images/" />
    <mvc:resources  mapping="/swf/**"    location="/resources/swf/"  />
    <mvc:resources  mapping="/resources/**"    location="/,classpath:/META-INF/publicResources/" />

    <!-- 配置拦截器 如果有多个拦截器满足拦截处理的要求,则依据配置的先后顺序来执行 -->
    <mvc:interceptors>
        <!-- 配置一个全局拦截器,拦截所有请求 -->
        <mvc:interceptor>
            <!-- 配置拦截器作用的路径 -->
            <mvc:mapping path="/**"/>
            <!-- 配置拦截器不拦截的路径 -->
            <mvc:exclude-mapping path="/toIndex" />
            <bean class="com.web.interceptor.GlobalInterceptor" />
        </mvc:interceptor>
        <mvc:interceptor>
            <!-- 配置拦截器作用的路径 -->
            <mvc:mapping path="/getAllUser"/>
            <bean class="com.web.interceptor.GetInfoInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>    
  
    <!--通用视图解析器 -->
    <bean id="viewResolverCommon"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"  value="org.springframework.web.servlet.view.InternalResourceView" />
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <!-- order属性指定当有多个ViewResolver的时候其处理视图的优先级 -->
        <property name="order" value="1" />
    </bean>
        
     
	 
    <!--
        Spring3.1开始,Spring内置了两个MultipartResolver的实现;
        StandardServletMultipartResolver;依赖于Servlet3.0对multipart请求的支持;
        CommonsMultipartResolver使用Jakarta Commons FileUpload解析multipart请求;
        一般来说,StandardServletMultipartResolver将会是优先选择的方案,它使用了Servlet所提供的原生功能支持,
        并不需要依赖任何第三方组件。如果要将应用部署到Servlet3.0之前的容器中,或者还没有使用Spring3.1或者更高的版本,
        那么可能就需要使用CommonsMultipartResolver了
    -->
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="11000000" />
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="uploadTempDir" value="/upload" />
        <!-- 其他属性自行添加 -->
    </bean>

    <!-- web.xml节点使用的版本必须是3.0及以上;限制上传大小和路径在前端控制器DispatcherServlet中配置 -->
    <!--    <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
    </bean>-->

    <!--
    以前DispatcherServlet并没有实现任何解析multipart请求数据的功能;直到Servlet3.0+提供了对文件上传的支持,
    通过@MultipartConfig标注和HttpServletRequest的两个新方法getPart()和getParts(),很方便实现文件的上传;
    根据实际情况选择上传的实现
    -->
  
</beans>
posted @ 2019-08-30 14:28  youngyajun  阅读(399)  评论(0)    收藏  举报