1,
RootConfig用来配置ContextLoadListener
1 @Configuration 2 //@Import(DataConfig.class) 3 @ComponentScan(basePackages={"spittr"}, 4 excludeFilters={ 5 @Filter(type=FilterType.CUSTOM, value=WebPackage.class) 6 }) 7 public class RootConfig { 8 public static class WebPackage extends RegexPatternTypeFilter { 9 public WebPackage() { 10 super(Pattern.compile("spittr\\.web")); 11 } 12 } 13 }
WebConfig 用来配置DispatcherServlet
1 @Configuration 2 @EnableWebMvc 3 @ComponentScan("spittr.web") 4 public class WebConfig extends WebMvcConfigurerAdapter { 5 6 @Bean 7 public ViewResolver viewResolver() { 8 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 9 resolver.setPrefix("/WEB-INF/views/"); 10 resolver.setSuffix(".jsp"); 11 return resolver; 12 } 13 14 @Override 15 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 16 configurer.enable(); 17 } 18 19 @Override 20 public void addResourceHandlers(ResourceHandlerRegistry registry) { 21 // TODO Auto-generated method stub 22 super.addResourceHandlers(registry); 23 } 24 25 }
创建上面2个context
public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
如果发现jsp中的对象展示没能正常工作,可能是web.xml没有配置到比较新的schema,应该是:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 8 9 </web-app>
原文:http://www.cnblogs.com/netact/p/6832902.html