• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)

原文地址: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加载两次,可以注释掉其中一边。


posted on 2018-04-23 17:07  红无酒伤  阅读(360)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3