springmvc servlet.xml

本文详细介绍了如何在Spring框架下利用注解驱动启动应用,通过自动扫描组件并将其注册为bean,简化配置过程。同时,还涵盖了视图解析器、视图映射关系、拦截器配置、国际化配置等内容。

在使用注解的情况下,系统启动时会被自动扫描,并添加到bean工厂中,省去了配置文件中写bean定义

1.启动mvc注解驱动

<mvc:annotation-driven />

2.组件扫描

<context:component-scan base-package="....">

在xml配置了这个标签后,spring可以自动去扫描base-package下面或者子包下面的java文件,如果扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean,不用在配置文件中配置bean

3.ViewResolver、View映射关系

4.拦截器配置

5.国际化配置

<bean id ="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">...

<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: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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                           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-3.0.xsd">
    <!-- 启动mvc注解驱动 -->
    <mvc:annotation-driven />
    <!-- 注解探测器 -->
    <context:component-scan base-package="spring.ioc.test">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    <!-- viewResolver & view映射关系 -->
    <bean id="viewResolverJSP"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="cache" value="false" />
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
        <property name="contentType">
            <value>text/html;charset=UTF-8</value>
        </property>
    </bean>
    <!-- 自定义拦截器配置 -->
    <mvc:interceptors>
    <mvc:interceptor>
    <mvc:mapping path="/hhtest/*"/>
    <bean class="spring.ioc.interceptor.MyInterceptor"></bean>
    </mvc:interceptor>
    </mvc:interceptors>
</beans>

 

转载于:https://www.cnblogs.com/yingyi/p/4332738.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值