首页 > spring mvc无法处理请求,404

spring mvc无法处理请求,404

RT,代码如下:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 自动扫包 -->
    <context:component-scan base-package="cn.ziav.ssshp.*">
        <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.RestController" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>
    <!-- 自动匹配AOP切面 -->
    <aop:aspectj-autoproxy />
    <!-- 1.配置数据源 -->
    <context:property-placeholder location="classpath*:db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:user="${user}" p:password="${password}" p:driverClass="${driver}"
        p:jdbcUrl="${url}" p:initialPoolSize="${initPoolSize}" p:maxPoolSize="${maxPoolSize}"
        p:maxIdleTime="300" p:maxIdleTimeExcessConnections="30"></bean>
    <!-- 2.配置JPA的EntityManagerFactory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:generateDdl="true" p:showSql="true"></bean>
        </property>
        <!-- 配置实体类所在的包 -->
        <property name="packagesToScan" value="cn.ziav.ssshp.*"></property>
        <property name="jpaProperties">
            <props>
                <!-- 二级缓存相关 -->
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
                </prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <!-- 读数据远大于写数据时启用 -->
                <!-- <prop key="hibernate.cache.use_minimal_puts">true</prop> -->
                <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
                    <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> -->
                <!-- hibernate 基本属性 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!-- 3.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

    <!-- 5.配置 SpringData -->
    <!-- 加入 jpa 的命名空间 -->
    <!-- base-package: 扫描 Repository Bean 所在的 package -->
    <jpa:repositories base-package="cn.ziav.ssshp.*"></jpa:repositories>
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="cn.ziav.ssshp.*" />
    <!-- 开启mvc -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <!-- 配置试图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".html" />
    <!-- 支持注解式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>ssshp</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

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


可否把你的项目目录结构贴出来瞧瞧?
照你这个错误情况,感觉是 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容器加载了


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

@RestController public class TestController {

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

这里加个日志就明白了

【热门文章】
【热门文章】