springmvc配置

来源:互联网 发布:dedecms中js如何使用 编辑:程序博客网 时间:2024/05/21 17:53
<?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:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描且只扫描@Controller -->

    <context:component-scan base-package="com.coamctech.sample.demo" use-default-filters="false">//默认情况下它是true的,即不仅扫描@controller,还要扫描@content的子注解@service,@repository
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 当返回值为字符串时, 为了避免乱码问题, 将默认字符集设置为UTF-8(默认是ISO-8859-1) -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" c:defaultCharset="UTF-8" />//spring默认的字符集是ISO-8859-1
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 定义JSP文件的位置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/"                           //controller中会返回一个字符串,而我们需要将其解析为视图的路径,因此通过对前缀和后缀的定义来定义视图的位置
        p:suffix=".jsp" />

    <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
    <mvc:default-servlet-handler />             //web.xml中定义了拦截所有请求,包括.js,.jpn等静态文件,因此我们需要定义一个默认的servlet来处理静态文件的访问
    

    <!-- 定义无需Controller的url<->view直接映射 -->
    <mvc:view-controller path="/" view-name="redirect:/index" />
    
    <!-- 处理异常 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
        p:exceptionMappings-ref="exceptionMappings" />
    
    <util:properties id="exceptionMappings">
        <prop key="java.lang.Throwable">error/500</prop>
    </util:properties>

</beans>
0 0
原创粉丝点击