spring mvc常用配置

<?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-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <mvc:annotation-driven validator="validator">
        <mvc:message-converters>
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4" >
                <property name="supportedMediaTypes">  
               <list>  <!-- 下面不配通过restful工具测试时会报错 -->
                      <value>text/plain;charset=utf-8</value>  
                      <value>text/html;charset=utf-8</value>  
                      <value>text/json;charset=utf-8</value>  
                      <value>application/json;charset=utf-8</value>  
                </list>  
           </property>
           </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

      <!--防止跨域请求出错-->

    <mvc:cors>
      <mvc:mapping path="/xxx/**" allowed-origins="*"/>
    </mvc:cors>

      <!--拦截器配置-->
    <mvc:interceptors>
      <mvc:interceptor>
        <mvc:mapping path="/xxx/**"/>
          <bean class="com.xxx.interceptor.SecurityInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- Freemarker配置 -->
    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/views/" />
        <property name="freemarkerSettings">
            <props>
                <prop key="defaultEncoding">UTF-8</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="localized_lookup">false</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="number_format">0.######</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                
            </props>
        </property>
    </bean>

    <mvc:view-resolvers>
        <bean 
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="contentType" value="text/html;charset=UTF-8" />
            <property name="cache" value="true" />
            <property name="prefix" value="" />   <!--此处配置和freemarkerConfig的相关配置路径是结合的 -->
<property name="suffix" value=".html" />
</bean>
<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp" />
</mvc:view-resolvers>

<context:component-scan base-package="xxx.yyyy.web" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- <property name="maxUploadSize" value="100000"/> -->
</bean>

</beans>
原文地址:https://www.cnblogs.com/hzhuxin/p/6674082.html