idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

时间:2024-03-21 15:46:14

引文:

在idea,maven项目搭建的SSM框架,遇到的问题是在Controller文件@RequestMapping加好注解,但是开了tomcat运行,一直进入不了Controller,报错500在404

 

介绍:

最近在学基于maven项目搭建的SSM框架,遇到的问题还挺多,其中搞得最难受的就是这个

org.springframework.web.servlet.DispatcherServlet报错500,在刷新就是报错404,网上也找个很多解决办法,包也都下好了,但是依然不行,最后突然想到好像WEB-INF下的lib没更新包,之前web项目会显示lib文件夹,但是这个maven项目建好web文件,lib就不显示了,然后我试了一下更新lib,就好了,我猜很多人也可能会遇到,也是给自己走过的坑记上一笔。

 

流程:

一、先保证有pom.xml有导入spring-web的包,以下是我的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hxh</groupId>
    <artifactId>SmartStockMavenSSM</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <!-- java ee jar包 -->
        <javaee.version>7.0</javaee.version>
        <!-- jstl jar包 -->
        <jstl.version>1.2</jstl.version>
        <!-- mysql-connector包 -->
        <mysql.version>8.0.15</mysql.version>
        <!-- commons-dbcp -->
        <dbcp.version>1.4</dbcp.version>
        <!-- mybatis-spring整合包 -->
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
        <!-- spring版本号 -->
        <spring.version>5.1.5.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis.version>3.4.6</mybatis.version>
        <!-- jackson版本号 -->
        <jackson.verson>2.9.8</jackson.verson>
        <!-- junit -->
        <junit-version>4.12</junit-version>
        <!-- cglib -->
        <cglib-version>3.2.2</cglib-version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.7.25</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <!-- 导入java ee jar 包 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee.version}</version>
        </dependency>

        <!-- JSTL标签类 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <!-- 导入Mysql数据库链接jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- dbcp数据库连接池包 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${dbcp.version}</version>
        </dependency>
        <!--mybatis-spring整合包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>
        <!-- mybatis-核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <!--spring-核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</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-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</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-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- jackson包 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.verson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.verson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.verson}</version>
        </dependency>

        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

        <!-- 测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <!-- cglib包 -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib-version}</version>
        </dependency>
        <!--日志记录的jar包 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>${slf4j.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- aspectjweaver包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
        <!-- aspectjrt 包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>

</project>

 

二、web.xml配置,一定要配置对<select-class>

<?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>
        <!-- 这里名字有讲究 如果不去修改spring配置文件默认位置
            那么springmvc它会去web-inf下找springmvc-servlet.xml文件
         -->
        <init-param>
            <!-- 上下文配置的位置的指定 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 下面的 会自动去类路径文件找springmvc.cml -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- servlet映射配置 -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 这里同一写斜杠 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

三、还有好多其他的配置,我这不是教学教程,就跳过,直接解决我说的lib更新jar包的图文教程

简洁流程:点击左上角File -> Project Structrue -> Artifacts -> Available Elements右击项目名 ->  Put into Output Root -> 检查左边的lib文件是否没有红色报错

下边图文教学

1. 点击左上角FIle

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

2. 点击Project Structrue

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

3. 点击Artifacts

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

4. 在右边有个Available Elements。右击项目名

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

5. 右击项目名,选择 Put into Output Root选项 会自动导入更新到左边WEB-INF下的lib文件

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

6. 完成,检查左边lib文件内是否还有红色的文件,如果还有就点击整个lib,在点击上边的减号”-“,然后在重新来一次,就可以了

idea maven SSM springmvc报错 org.springframework.web.servlet.DispatcherServlet 先500在404,已解决!!!

 

 

最后

这是第二次写CSDN,写的依然乱七八糟,希望该表达的意思大家可以理解,如果有什么问题可以在评论中写出来,可以互相交流一下,最后我推荐一个学习SSM框架的视频,很好的教学,谢谢大家的浏览啦!

SSM教学视频bilibili