首页 > 编程语言 > 详细

Spring MVC 配置

时间:2017-05-09 23:54:53      阅读:366      评论:0      收藏:0      [点我收藏+]

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 }
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   @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 }
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 配置

原文:http://www.cnblogs.com/netact/p/6832902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!