原文地址:SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI
采用 spring 零配置,参考 http://hanqunfeng.iteye.com/blog/2113820,WebInitializer 类代码如下:
public class WebInitializer implements WebApplicationInitializer { private static final Logger logger = LogManager.getLogger(WebInitializer.class); @Override public void onStartup(ServletContext servletContext) throws ServletException { logger.info("-------->启动web应用"); /* * Log4jConfigListener: log4j 2日志配置,配置文件为 classpath:log4j2.xml */ servletContext.addListener(new Log4jServletContextListener()); Log4jServletFilter log4jServletFilter = new Log4jServletFilter(); servletContext.addFilter("log4jServletFilter", log4jServletFilter); /* * OpenSessionInViewFilter: 加上该 filter 可以保证jackson能够序列化LAZY FetchType */ OpenSessionInViewFilter openSessionInViewFilter = new OpenSessionInViewFilter(); FilterRegistration.Dynamic openSessionInViewFilterRegistration = servletContext .addFilter("openSessionInViewFilter", openSessionInViewFilter); //指定在spring配置的sessionFactory openSessionInViewFilterRegistration.setInitParameter("sessionFactoryBeanName", "sessionFactory"); //singleSession默认为true,若设为false则等于没用OpenSessionInView openSessionInViewFilterRegistration.setInitParameter("singleSession", "true"); openSessionInViewFilterRegistration.addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, "/"); ServerParameter params = ServerParameter.getInstance(); DBMananger.loadEnv(); if(DBMananger.connDB()) { // 数据库连接成功,再载入APP配置 params.addAttribute("connDBSataus", true); /* * spring mvc 外的配置 AppConfig类 */ AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); } /* * spring-mvc 上下文 */ AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext(); // springMvcContext.register(MvcConfig.class); /* * DispatcherServlet */ DispatcherServlet dispatcherServlet = new DispatcherServlet(springMvcContext); ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet); dynamic.setLoadOnStartup(1); dynamic.addMapping("/"); /* * CharacterEncodingFilter: 解决中文乱码问题 */ CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("utf-8"); FilterRegistration characterEncodingfilterRegistration = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter); characterEncodingfilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); } }
MvcConfig 配置如下:
public class MvcConfig extends WebMvcConfigurationSupport { private static final Logger logger = LogManager.getLogger(MvcConfig.class); /** * 注册视图处理器 * @return */ @Bean public ViewResolver viewResolver() { logger.info("注册视图处理器-------->ViewResolver"); InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/jsp/"); viewResolver.setSuffix(".jsp"); return viewResolver; } /** * 资源访问处理器 * 可以在jsp中使用/images/**的方式访问/images/下的内容 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { logger.info("资源访问处理器-------->addResourceHandlers"); registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .resourceChain(true).addResolver( new VersionResourceResolver().addContentVersionStrategy("/**")); } }
结果出现No mapping found for HTTP request with URI警告,访问静态资源时为404错误。
经调试发现,在MvcConfig类中加入下面Bean,问题解决
@Bean public HandlerMapping resourceHandlerMapping() { logger.info("HandlerMapping"); return super.resourceHandlerMapping(); }
为什么要显式声明该Bean,尚未得知。
另外,如果在MvcConfig类中加注解,@Configuration,在WebInitializer类中配置
AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext(); springMvcContext.register(MvcConfig.class);
将会导致MvcConfig加载两次,可以注释掉其中一边。
原文地址:https://www.cnblogs.com/huanghongbo/p/8920318.html
时间: 2024-10-15 07:33:05