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



原文地址:https://www.cnblogs.com/huanghongbo/p/8920318.html

时间: 2024-10-15 07:33:05

SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)的相关文章

SpringMvc出现No mapping found for HTTP request with URI的终极解决办法

No mapping found for HTTP request with URI 出现这个问题的原因是在web.xml中配置错了,如: <servlet> <servlet-name>springMVCDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init

[org.springframework.web.servlet.PageNotFound] -No mapping found for HTTP request with URI [/homework/js/jquery-1.7.2.min.js] in DispatcherServlet with name &#39;SpringMVC&#39;

先上解决方案:对于上面出现的问题,查看了很多相关的博客,论坛.最后实在Spring-mvc.xml这个配置文件里加入了如下两行: 1 <mvc:annotation-driven /> 2 <mvc:default-servlet-handler /> 问题描述: 由于项目需要Ajax,于是在jsp的head里加入了如下代码: 1 <script type="text/javascript" src="/homework/js/jquery-1.

java springmvc No mapping found for HTTP request with URI

最近,想写一个全文搜索的工具,想着就是用springMVC+lucene+tika来完成,当把整个架构都搭建好了以后,发现springmvc配置的control总是映射不到,总是报No mapping found for HTTP request with URI,导致无法执行逻辑层: 经过两天的不断尝试(期间spring源码都翻看了一些,而且通过调试spring代码找原因,最终也没找到),偶然之间觉得使用maven的clean和install的方式打成war试了一下,居然问题不存在了.考虑了一

Spring3中js/css/jpg/gif等静态资源无法找到(No mapping found for HTTP request with URI)问题解决(转)

最近项目中使用到Spring3,在感叹Spring3注解配置清爽的同时竟然出现了这个不和谐的事情,实在无法忍受 问题:部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52:51,646 WARN [org.springframework.web.servlet.PageNotFound] -<No mapping found for HTTP request with URI [/sandDemo001/images/1.jpg] in DispatcherServ

No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name &#39;HelloWeb&#39; Spring MVC

I'm learning the Spring Framework, and I'm doing the HelloWeb tutorial on tutorialspoint, and I can't get it working. I'm using Spring MVC 4.0, and I'm deploying my app from Netbeans 8.0 to a Glassfish Server. http://www.tutorialspoint.com/spring/spr

相对路径获取项目文件 及报错 No mapping found for HTTP request with URI XXX in DispatcherServlet with name ‘springmvc’解决方法

首先一点,WebRoot目录下的文件是都可以通过浏览器输入路径,直接读取到的 例如这样: 而WebRoot下面WEB-INF是无法浏览器输入路径直接读取的. 因为是受保护的. 如果jsp读取一个图片的话. jsp在WebRoot/jsp下 图片在WebRoot/imag下 那么就要../imag/xx.jpg获取相对路径. 因为servlet程序是无法读取C:\xx\xx\xx这样的绝对路径的. 除非更改tomcat配置文件,但我觉得那样完全没有意义. 另外,要注意你请求的路径是否被拦截. 例如

No mapping found for HTTP request with URI

新在学习Spring3.0,AngularJS和Bootstrap时,一直在考虑如果把这三者结合起来.今天在尝试的过程中,遇到了拦路虎.No mapping found for HTTP request with URI [/springmvc/js/bootstrap.min.js] web.xml的配置如下 <servlet-mapping> <servlet-name>mumu</servlet-name> <url-pattern>/</url

springmvc No mapping found for HTTP request with URI in Dispatc

项目是使用spring MVC (1)在浏览器中访问,后台总报错: Java代码   No mapping found for HTTP request with URI [/exam3/welcome] in DispatcherServlet with name 'spring2' 查了好半天,才发现是controller 没有扫描到. 我是使用的注解. spring mvc配置文件如下: Xml代码   <?xml version="1.0" encoding="

springMVC No mapping found for HTTP request with URI

转载自:http://blog.sina.com.cn/s/blog_534f69a00101332u.html 1.问题: No mapping found for HTTP request with URI 出现这个问题的原因是在web.xml中配置错了,如: <servlet> <servlet-name>springMVCDispatcherServlet</servlet-name> <servlet-class>org.springframewo