首页 > 代码库 > 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 }
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 }
创建上面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>
Spring MVC 配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。