首页 > 代码库 > Spring MVC 配置

Spring MVC 配置

1, 

RootConfig用来配置ContextLoadListener

技术分享
 1 @Configuration 2 //@Import(DataConfig.class) 3 @ComponentScan(basePackages={"spittr"},  4     excludeFilters={ 5         @Filter(type=FilterType.CUSTOM, value=http://www.mamicode.com/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 }
RootConfig

 

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   @Override15   public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {16     configurer.enable();17   }18   19   @Override20   public void addResourceHandlers(ResourceHandlerRegistry registry) {21     // TODO Auto-generated method stub22     super.addResourceHandlers(registry);23   }24 25 }
WebConfig

 

创建上面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[] { "/" };  }}
SpitterWebInitializer

 

如果发现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>

 

Spring MVC 配置