Spring注解(八):servlet与Springmvc整合

  1. 创建maven项目,由于没有web.xml文件,需要在maven中添加以下内容
     <build>
      	<plugins>
      		<plugin>
      			<groupId>org.apache.maven.plugins</groupId>
      			<artifactId>maven-war-plugin</artifactId>
      			<version>2.4</version>
      			<configuration>
      				<failOnMissingWebXml>false</failOnMissingWebXml>
      			</configuration>
      		</plugin>
      	</plugins>
      </build>
  2. 添加jar包:
     <dependencies>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-webmvc</artifactId>
      		<version>4.3.11.RELEASE</version>
      	</dependency>
      	
      	<dependency>
      		<groupId>javax.servlet</groupId>
      		<artifactId>servlet-api</artifactId>
      		<version>3.0-alpha-1</version>
                    <!-- 打包时不添加依赖 -->
      		<scope>provided</scope>
      	</dependency>
      </dependencies>

     

  3. 继承AbstractAnnotationConfigDispatcherServletInitializer类
    package indi.com;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    import indi.com.config.AppConfig;
    import indi.com.config.RootConfig;
    
    //web容器启动时创建对象,调用方法来初始化容器,以前前端控制器
    public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    	//获取根容器的配置类:(Spring的配置文件)父容器
    	@Override
    	protected Class<?>[] getRootConfigClasses() {
    		// TODO Auto-generated method stub
    		return new Class<?>[]{RootConfig.class};
    	}
    
    	//获取web容器的配置类:(Spring的配置文件)子容器
    	@Override
    	protected Class<?>[] getServletConfigClasses() {
    		// TODO Auto-generated method stub
    		return new Class<?>[]{AppConfig.class};
    	}
    
    	//获取DispatcherServlet的映射信息
    	//  /:拦截所有请求(包含静态资源(xx.js,xx.png)),但不包含*.jsp
    	//  /*:拦截所有请求,连*.jsp页面都拦截,jsp页面时tomcat的jsp引擎解析的
    	@Override
    	protected String[] getServletMappings() {
    		// TODO Auto-generated method stub
    		return new String[]{"/"};
    	}
    
    }

     

  4. 定制web容器
    package indi.com.config;
    
    
    import java.util.List;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.format.FormatterRegistry;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.MessageCodesResolver;
    import org.springframework.validation.Validator;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
    import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import indi.com.controller.MyFirstInterceptor;
    
    //SpringMVCֻ只扫描Controller:子容器
    //useDefaultFilters=false ����Ĭ�ϵĹ��˹���
    @ComponentScan(value="indi.com",includeFilters={
    		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
    },useDefaultFilters=false)
    @EnableWebMvc
    public class AppConfig  extends WebMvcConfigurerAdapter  {
    
    	//定制
    	
    	//视图解析器
    	@Override
    	public void configureViewResolvers(ViewResolverRegistry registry) {
    		// TODO Auto-generated method stub
    		//默认所有的页面都从WEB-INF/ xxx .jsp
    		//registry.jsp();
    		registry.jsp("/WEB-INF/views/", ".jsp");
    	}
    	
    	//静态资源访问
    	@Override
    	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    		// TODO Auto-generated method stub
    		configurer.enable();
    	}
    	
    	//拦截器
    	@Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		// TODO Auto-generated method stub
    		//super.addInterceptors(registry);
    		registry.addInterceptor(new MyFirstInterceptor()).addPathPatterns("/**");
    	}
    
    }

     

  5. 定制根容器
    package indi.com.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.stereotype.Controller;
    
    //Spring根容器,不扫描controller;
    @ComponentScan(value="indi.com",excludeFilters={
    		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
    })
    public class RootConfig {
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值