ssm的springmvc配置文件分析

springmvc文件配置

1.定义注解扫描的包

<context:component-scan base-package="com.xpc.controller"></context:component-scan>

2.试图解析器

如果使不用转发就不需要配置试图解析器

    <!--内部视图资源解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--拼接返回页面的前缀路径-->
        <property name="prefix" value="/static/"></property>
        <!--拼接返回页面的后缀路径-->
       <property name="suffix" value=".html"></property>
    </bean>

3.解决返回数据乱码问题

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                    <value>text/plain;charset=UTF-8</value>
                    <value>application/xml;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

4.配置静态资源解析器

默认情况下springmvc处理不了静态资源,当springmvc处理不了静态资源的时候就需要配置

<mvc:resources mapping="/static/js/**" location="/static/js/" ></mvc:resources>
    <mvc:resources mapping="/static/**" location="/static/" ></mvc:resources>

5.处理文件上传

如果要完成文件上传功能,需要配置

CommonsMultipartResolver帮我们接受文件,存储到临时目录 p:uploadTempDir=“file:/d:/temp”

<bean id="multipartResolver"    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
      p:defaultEncoding="UTF-8"
      p:maxUploadSize="5242880"
      p:uploadTempDir="file:/d:/temp"
/>

6.定义拦截器

如果要对请求进行拦截,可以配置

<!--定义拦截器  用MyInterceptor类拦截 /user/**路径 -->
<mvc:interceptors>
<mvc:interceptor>
    <mvc:mapping path="/user/**"/>
    <bean class="com.xpc.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:interceptor>
    <mvc:mapping path="/user/**"/>
    <bean class="com.xpc.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

猜你喜欢

转载自blog.csdn.net/Riding_ants/article/details/106712357