SpringMVC项目出现404

目录

问题讲解:

解决方案:

1、处理器映射器和处理器适配器以及视图解析器没有配置好

2、Controller的包扫描没有加或者包扫描的配置是错误的

3、当然也有说jar包没有

4、请求地址是错误的

5、还有一种解决办法说web.xml配置DispatcherServlet的时候servlet的映射关系配置不正确,

解决方案思路:

 1、分析问题原因

           1.1、前台访问路径:

           1.2、后台映射路径:

 2、解决问题步骤

  2.1、前台访问路径:

 2.2、从哪里映射,映射到哪里去:

 2.3、这里是重点,看他是怎么映射的。

映射器、适配器和视图解析器:

    2.4、如果说前面这些都配置好了,问题还是存在,我们去检查这些配置文件有没有一步步的都加载到spring容器中。

 2.5、如果以上都配置好了,还是不成功,那么就得在看看这些自动化的东西到底有没有自动化成功,

 2.6、如果说controller配置没有错误,那就看看是不是jar包的问题,记得在工程发布的路径下查看。

 2.7、如果说还不成功,那就只能继续深入看源码了。还有一个方法就是启动的时候使用DEBUG模式,且在发布时去掉autoloading enabled,这样如果是源码问题就会卡出来,可以看到提示。不过相信走到这一步基本也就把错误排查出来了,一般不会去看源码提示,因为除非导包的问题,源码不会有错。


问题讲解:

        从标题不难看出,框架搭建好后,编译部署并启动项目一切正常,并没有报任何的错误,但是从页面访问controller时缺报404[Not Found]的错误。

解决方案:

   这里我总结一下网上各种帖子的解决方案:

1、处理器映射器和处理器适配器以及视图解析器没有配置好

         1.1、非注解的处理器映射器和适配器配置,注意:在这里处理器映射器的配置必须和处理器适配器的配置是成对的,还有必须在配置文件里声明注入你的controller,并且这个controller必须实现Controller接口。非注解的配置访问是根据声明注入的bean name来进行映射处理的,必须有name,而且name的值必须有根符号(“/”)。

<bean name="/userController" class="com.aqiuo.controller.UserController"></bean>

        1.2、注解的处理器映射器和适配器配置。注解的controller类必须使用@Controller进行注解,类中的方法使用@RequestMapping("")进行注解。注解的配置方式还有一种,即<mvc:annotation-driven/>注解驱动配置,这样更方便,实际项目也是这样使用的。 

@Controller
public class ClassController {

    @Autowired
    IClassService classService;

    /**
     * 增加班级
     */
    @RequestMapping("/addClass")
    @ResponseBody
    public Result addClass(@RequestBody ClassVo classVo){

        return classService.addClass(classVo);

    }

        1.3、视图解析器的配置。

    <!--视图解析器:DispatcherServlet给他的Model And ivew-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!--前缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

        至此,springmvc的controller需要的处理器映射器和处理器适配器以及视图解析器的配置已经完成,当然实际要运行的时候还必须要有Controller包扫描,不然你的程序在实例化完sqlSessionFactory后会一直卡在那里,不做任何操作,页面也不会有任何的反映。

2、Controller的包扫描没有加或者包扫描的配置是错误的

这如果说其他配置都OK的话,如果没有加包扫描,那么他不回报任何的错误,但是会输出日志,创建SqlSessionFactory的日志是有的,不会什么信息都没有。有的人将Controller的包扫描加成这样

<context:component-scan base-package="com.springmvc.ssm.controller.*"/>

也是什么都没有的。正确的包扫描是这样的:

  <context:component-scan base-package="com.aqiuo.controller"/>

3、当然也有说jar包没有

这也有可能是有的,没有jar包报404很正常的。这里要说的是如果出现404,你的判断是jar包没有,那你就得去你的项目发布路径下的WEB-INF下的lib文件夹去找jar包,而不是你的idea里看有没有。

        maven可能会出现jar包导不进去的问题,自己去projectstruction内看一看

4、请求地址是错误的

        这个只能说自己认真排查,一般人在出现404的时候第一反应就是请求地址的核对,所以这个我也不说了。

        但是在这里我强调一个现象,就是你的请求地址是正确的,但是你的目录里没有Controller里的方法返回的页面,这样也会出现404,但是这个404和之前的404是不一样的。比如我在我的Controller的aa/hello映射的方法里返回的,实际我的jsp下的JSP文件没有一个牛.jsp,那么页面的错误就会是这样的:(你想返回数据,但是你没有加@ResponseBody,导致实际按返回视图,但视图又没有所以404)

@Controller
@RequestMapping("/aa")
public class StudentController {
    @RequestMapping("/hello")

    public String ressda(){
        System.out.println(666);
        return "牛";
    }

}

5、还有一种解决办法说web.xml配置DispatcherServlet的时候servlet的映射关系<url-pattern>配置不正确,

        应该将<url-pattern>/*</url-pattern>改为<url-pattern>/</url-pattern>,去掉根号后面的星号,当然如果确实这样写了,那这也是一种解决办法。

      以上就是网上所说的解决这一问题的办法,其实也是我遇到这样的问题,在网上一通乱搜,挨个试了个遍。

        那么网上这么多的解决方法,我们到底应该去按哪个方法执行,像我一样挨个试一遍?就跟我一样,全部试完也不见得能解决你的问题,因为每个人的问题发生的原因都不一样,但是结果都一样,访问Controller失败,返回404。这也就是我写这篇文章的真正原因,我在这里不会贴出我解决该问题的代码,没有用对你,也许你的问题按照我的方法解决不了。那么我在这里将这个问题的解决思路抽取出来,这样大家就不用在网上搜各种解决方案还不一定适合你,只要按着这个思路找问题的根源,就不会有多大的问题。

解决方案思路:

 1、分析问题原因

            我们既然知道是404,那就肯定是路径的问题,至少在现有的开发环境中大多数404都是各种路径的问题,不管是没有对应的文件还是路径写错了。既然404是路径问题,那么我们再细化这个路径问题,把这个路径可以分为前台访问路径和后台映射路径。

           1.1、前台访问路径:

        前台访问路径不用说就是我们在地址栏输入的路径,即http请求路径。

           1.2、后台映射路径:

        那么后台映射路径就多了,分析这个问题其实也很简单,既然是映射路径,那么他是怎么映射的,从哪里映射的,映射到哪里去,完成这三个问题也就解决了这个问题了。

                    1.2.1、在哪里映射、映射到哪里去:其中从哪里映射和映射到那里去,这个问题很好回答,既然是springmvc,那么这个问题肯定在Controller里了,当然这里的映射到那里去也要根据你自己配置视图解析器的规则去写。

                    1.2.2、怎么映射:然后我们分析这个路径是怎么映射的,这里就有人说了,一个刚学的就去研究spring源码吗?其实我们一般情况下没必要去翻源码查看他的实现原理,但是你如果有能力看他的源码,会加深你对springmvc的理解,对你也是有益无害的。虽然说我们没有必要翻源码但是必须知道他的映射关系是怎么配置的,弄清楚这些,我相信你的问题也就解决了。

 2、解决问题步骤

             我们已经把这个问题细化了,那么就一步步来解决就行了,在这里我会穿插的将我的完整代码贴出来,大家可以参考,初学者直接拿去也是可以运行的。在这里我先把我的工程目录给大家放在这里。

  2.1、前台访问路径:

        这个没有什么,只能是仔细的比对的你的请求路径是否正确,从项目工程名到到映射的方法名,再到返回的jsp名称。

             

 2.2、从哪里映射,映射到哪里去:

        这个问题我们就考虑Controller类中映射是不是不正确,查看@RequestMapping("/映射地址")中的映射地址是否与访问地址对应。返回路径是否正确,例如返回consigner,在视图解析器配置的路径规则下能不能找到consigner.jsp文件。

      

 2.3、这里是重点,看他是怎么映射的。

        我们知道springmvc是有前端控制器来调配映射器和适配器去工作的,那么我们首先要确保前端控制器配置正确。前端控制器配置好了,还需要处理器映射器和适配器以及视图解析器都配置好才可以。

各种处理器,映射器,解析器配置配置下面可以参考!!

映射器、适配器和视图解析器:

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd
             http://www.springframework.org/schema/mvc
              https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--   扫描注解 -->
    <context:component-scan base-package="com.aqiuo.controller"/>
<!--静态资源交给servlet处理,不是静态资源才交给DispatcherServlet-->
    <mvc:default-servlet-handler />

<!--加载处理器映射器,处理器适配器等bean-->
    <mvc:annotation-driven></mvc:annotation-driven>


    <!--视图解析器:DispatcherServlet给他的Model And ivew-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!--前缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>


前端控制器配置:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-config.xml</param-value>
    </init-param>

  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--访问:DispatcherServlet这是入口,格外注意-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--编码过滤器-->
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!-- 配置加载spring文件的监听器 -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>



</web-app>

   
 2.4、如果说前面这些都配置好了,问题还是存在,我们去检查这些配置文件有没有一步步的都加载到spring容器中。

        那么我们要在web.xml里面加载那些东西呢?因为我是SSM框架,所以spring的配置文件,springmvc的配置文件,mybatis的配置文件,mysql数据库的配置文件,spring和mybatis整合的配置文件等都要加载到spring容器中。当然每个人写配置文件的方式不一样,加载的方式也不一样,但是都有一定的约定必须遵循。(我的配置习惯是学习的狂神)

        我是这样加载的:大家可以看的web.xml配置文件,我在这里面加载了spring配置文件和springmvc的配置文件以及spring和mybatis整合的配置文件。现在将另外几个配置文件贴出来:
           spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <import resource="spring-mapper.xml"></import>
    <import resource="spring-service.xml"></import>



</beans>

 spring-mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

    <context:component-scan base-package="com.aqiuo.mapper"></context:component-scan>
    <!--
     dbcp:手动配置
      c3p0自动化操作
     druid
     -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!--配置DAO接口扫描包,动态的实现Dao接口可以注入Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.aqiuo.mapper"></property>

    </bean>

</beans>

 spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!--扫描service包下注解-->
    <context:component-scan base-package="com.aqiuo.service"></context:component-scan>


    <!--声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

mybatis-config,xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <typeAliases>
        <package name="com.aqiuo.pojo"/>
        <package name="com.aqiuo.vo"/>
        <package name="com.aqiuo.dto"/>
    </typeAliases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

</configuration>

springmvc-config.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd
             http://www.springframework.org/schema/mvc
              https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--   扫描注解 -->
    <context:component-scan base-package="com.aqiuo.controller"/>
<!--静态资源交给servlet处理,不是静态资源才交给DispatcherServlet-->
    <mvc:default-servlet-handler />


<!--加载处理器映射器,处理器适配器等bean-->
    <mvc:annotation-driven></mvc:annotation-driven>


    <!--视图解析器:DispatcherServlet给他的Model And ivew-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!--前缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>


   

 2.5、如果以上都配置好了,还是不成功,那么就得在看看这些自动化的东西到底有没有自动化成功,

        例如service的自动扫描有没有起作用,Controller的自动扫描有没有起作用或者说是配置正确不正确。我的Controller的自动扫描是在spring.xml文件完成的。


 2.6、如果说controller配置没有错误,那就看看是不是jar包的问题,记得在工程发布的路径下查看。
 2.7、如果说还不成功,那就只能继续深入看源码了。还有一个方法就是启动的时候使用DEBUG模式,且在发布时去掉autoloading enabled,这样如果是源码问题就会卡出来,可以看到提示。不过相信走到这一步基本也就把错误排查出来了,一般不会去看源码提示,因为除非导包的问题,源码不会有错。

        最后说一下我的错误吧,困了我一周的事件。

        看我原来url-pattern的值写的是*.action,导致不匹配的模式串匹配不上找不到。所以大多数的错误都是人为的,而且是不起眼的小问题,可就是这些不起眼的小问题,总是折腾人。        

  • 32
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五敷有你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值