springmvc项目,浏览器报404错误的问题

时间:2022-12-02 08:11:46

问题描述:

建立了web工程,配置pom.xml,web.xml,编写controller类,在spring-mvc-servlet.xml文件中指定开启注解和扫描的包位置<mvc:annotation-driven /> <context:component-scan base-package="controller" />.打开tomcat,在浏览器中访问,提示404错误

问题分析:

一. 确定配置文件是否配置正确(springmvc的配置文件,web.xml和pom.xml基本不会配置出错)

因为提示的是404错误,可以确定正确加载了配置文件,处于排查需要,还是检查了配置文件的配置问题.
a) 放在WEB-INF/文件夹下,名称为spring-mvc-servlet.xml,不需要在web.xml中配置,系统自动读取
b) 放在WEB-INF/文件夹下,名称为自定义,或者放在src/main/resource文件夹下,需要在web.xml中配置,
配置代码如下:

    <servlet>  
        <servlet-name>dispatch</servlet>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/applicationContext.xml</param-value>  
        </init-param>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>dispatch</servlet-name>  
        <servlet-pattern>*.*</servlet-pattern>  
    </servlet-mapping>  

二. 确定maven 依赖正确配置
a) 一般使用两个依赖就能使用注解配置springmvc项目,并能正常运行
pom中的依赖如下:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.6.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
    </properties>
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
  </dependencies>
b) 我使用spring+springmvc+hibernate的搭配,pom依赖如下:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.6.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- springframework 4 dependencies begin -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- springframework 4 dependencies end -->

        <!-- log -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- hibernate 配置 begin -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.common</groupId>
                <artifactId>hibernate-commons-annotations</artifactId>
                <version>4.0.5.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <version>1.0.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-ehcache</artifactId>
                <version>${hibernate.version}</version>
                <!-- hibernate 配置 end -->
            </dependency>
            <!-- mysql数据库的驱动包 start -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.6</version>
            </dependency>
            <!-- 数据连接池 -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-c3p0</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

我已经依赖了a中所说的两个包,但是不能在浏览器中访问,会报404错误.

解决办法:

看了很多spring mvc的maven依赖,发现有人使用如下依赖:

     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency> 
    

我在b中的pom文件中加入这个依赖,就能正常访问了,不知道什么原因.(狗屁问题,浪费了我两个小时的时间,一度怀疑人生)