- 创建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>
- 添加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>
- 继承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[]{"/"}; } }
- 定制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("/**"); } }
- 定制根容器
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 { }
Spring注解(八):servlet与Springmvc整合
最新推荐文章于 2022-08-14 17:46:44 发布