java - spring mvc无法处理请求,404
天蓬老师
天蓬老师 2017-04-17 17:37:34
[Java讨论组]

RT,代码如下:

applicationContext.xml



    
    
        
        
    
    
    
    
    
    
    
    
        
        
            
        
        
        
        
            
                
                org.hibernate.cache.ehcache.EhCacheRegionFactory
                
                true
                true
                
                
                
                
                org.hibernate.dialect.MySQL5InnoDBDialect
                true
                true
                update
            
        
    
    
    
        
    

    
    
    
    

spring-mvc.xml



    
    
    
    
    
    
    
    
    

web.xml



    ssshp
    
        contextConfigLocation
        classpath*:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    
    
        dispatcher
        /
    
    
        index.html
    

TestController.java

@RestController
public class TestController {

    @RequestMapping("/test/{test}")
    public String test(@PathVariable String test) {
        return test;
    }
}

请求地址为:

http://localhost:8081/ssshp/test/jkljkle...

返回结果:

HTTP ERROR 404

Problem accessing /ssshp/test/jkljklew. Reason:

Not Found Powered by Jetty:// 9.3.8.v20160314

但是请求地址改成

http://localhost:8081/ssshp/index.html

可以正常显示。。。
这个问题网上搜了两天,没发现哪里错了,心累,各位大大求解QAQ

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
伊谢尔伦

因为jkljklew.html页面不存在啊。

@RestController public class TestController {

@RequestMapping("/test/{test}")
public String test(@PathVariable String test) {
    return test;
} }

这里加个日志就明白了

PHP中文网

可否把你的项目目录结构贴出来瞧瞧?
照你这个错误情况,感觉是 index.html 是在发布目录的根目录下的,没有走 DispatcherServlet,所以可以访问, 上面一个报错你看是不是 因为 no handler,应该是Controller没有被扫描到吧。

我尝试了下,自己也走了个大坑,终于出来了。

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-servlet.xml</param-value>
    </init-param>
我的配置文件,但是目录结构是我把 spring-servlet.xml 放在了  conf 目录下,
导致配置文件没被扫描到,所以404了。(记得看过一句说是 classpath*: 
会去 classpath 下搜寻所有匹配的文件,我理解成只要写文件名,
spring会自动去classes目录以及子目录搜文件,今天出错后又去好好看了下,
才发现理解错了,具体百度 classpath classpath* 就有好多)

如果你的错不是这个的话,把项目拿出来看看吧,就这点信息看解决不了错误

对了,还有个好东西: BeanPostProcessor
你百度下就知道了。 可以用来检测你的bean是否被spring IoC容器加载了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号