SpringMVC的配置文件

来源:互联网 发布:tcpip网络层安全协议 编辑:程序博客网 时间:2024/05/29 02:37

一、配置头部

<?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:mvc="http://www.springframework.org/schema/mvc"    xmlns:p="http://www.springframework.org/schema/p" 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.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.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

二、配置注解驱动设置、静态资源访问

<mvc:annotation-driven /><!--<mvc:resources location="/static/" mapping="/static/**" /> -->    <mvc:resources location="/" mapping="/**" />

三、 解决org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest问题

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

四、启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题

<bean        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <bean                    class="org.springframework.http.converter.StringHttpMessageConverter">                    <property name="supportedMediaTypes">                        <list>                            <value>text/html;charset=UTF-8</value>                        </list>                    </property>                </bean>            </list>        </property>    </bean>

五、 配置freemarker视图模版路径

<bean id="freemarkerConfigurer"        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">        <property name="templateLoaderPath" value="/application/admin" />        <property name="defaultEncoding" value="UTF-8" />        <property name="freemarkerSettings">            <props>                <prop key="template_update_delay">10</prop>                <prop key="locale">zh_CN</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>                <prop key="number_format">#.##</prop>            </props>        </property>    </bean>

六、配置FreeMark视图解析器

<bean id="freeMarkerViewResolver"        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">        <property name="contentType" value="text/html;charset=UTF-8" />        <property name="viewClass"            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />        <property name="suffix" value=".ftl" />        <property name="cache" value="true" />        <property name="exposeSessionAttributes" value="true" />        <property name="exposeRequestAttributes" value="true" />        <property name="exposeSpringMacroHelpers" value="true" />        <property name="allowSessionOverride" value="true" />        <property name="order" value="1" />    </bean>

七、扫描@Controller注解的类,完成bean创建和依赖注入 (spring Mvc层)

    <context:component-scan base-package="com.qsy" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />    </context:component-scan>
原创粉丝点击