您的位置:首页 > 编程语言 > Java开发

springmvc配置

2015-08-31 15:24 423 查看
<?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-se
4000
rvlet-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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: