简体   繁体   English

ClassCastException:org.springframework.web.servlet.DispatcherServlet无法转换为javax.servlet.Servlet

[英]ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet

I have created a new Spring MVC project using maven-eclipse and the following error is thrown: 我使用maven-eclipse创建了一个新的Spring MVC项目,并引发以下错误:

(I tried some solutions from the stackoverflow which is not working in my case. Some problem with the pom.xml which I could not find. I added provided scope for the servlet-api and tried which was not working either.) (我尝试了stackoverflow的一些解决方案,但这些解决方案在我的情况下不起作用。pom.xml出现了一些我找不到的问题。我为servlet-api添加了provided作用域,并尝试了它也不起作用。)

SEVERE: Servlet /Remindem threw load() exception
java.lang.ClassCastException: 
org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet

My pom.xml 我的pom.xml

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

My web.xml 我的web.xml

contextConfigLocation /WEB-INF/classes/config/spring/*-context.xml contextConfigLocation /WEB-INF/classes/config/spring/*-context.xml

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/config/spring/servlet_context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Try to change the config as it follows. 尝试按照以下步骤更改配置。

<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/classes/config/spring/servlet_context.xml
    </param-value>
</init-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

DispatcherServlet does not need init-param by default use DispatcherServlet默认情况下不需要init-param

XXXX-servlet.xml where XXXX is the name of the servlet XXXX-servlet.xml,其中XXXX是servlet的名称

Be sure these dependencies are in your pom.xml 确保这些依赖项在您的pom.xml中

 <!-- Spring dependencies section -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

Remove the servlet-api form your pom.xml, as it will be available in server. 从pom.xml中删除servlet-api,因为它将在服务器中可用。

Here is my pom.xml: http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.mkyong CounterWebApp war 1.0-SNAPSHOT CounterWebApp Maven Webapp http://maven.apache.org 1.8 3.0.5.RELEASE</spring.version> --> 4.1.1.RELEASE 1.2 4.11 1.0.13 1.7.5 这是我的pom.xml:http://maven.apache.org/maven-v4_0_0.xsd“> 4.0.0 com.mkyong CounterWebApp war 1.0-SNAPSHOT CounterWebApp Maven Webapp http://maven.apache.org 1.8 3.0。 5.RELEASE </spring.version>-> 4.1.1.RELEASE 1.2 4.11 1.0.13 1.7.5

<dependencies>
    <!-- Unit Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>
    <!-- Spring Core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--Binding for commons logging over slf4j-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${jcl-over-slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>       
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>       
     <!-- Spring dependencies section -->
    <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>
    <!-- jstl -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl.version}</version>
    </dependency>
</dependencies>

<build>
    <finalName>CounterWebApp</finalName>
    <plugins>
        <!-- Eclipse project -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <!-- Always download and attach dependencies source code -->
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
                <wtpversion>2.0</wtpversion>
            </configuration>
        </plugin>
        <!--Set JDK Compiler Level -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <!-- <version>2.3.2</version> 3.3 -->
            <version>3.5.1</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
            <!-- For Maven Tomcat Plugin -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/CounterWebApp</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Run the tomcat from cmd line: mvn tomcat:run 从cmd行运行tomcat:mvn tomcat:run

Note: you should stop the tomcat server running in the eclipse. 注意:您应该停止在Eclipse中运行的tomcat服务器。

This one worked for me. 这个为我工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:class org.springframework.web.servlet.DispatcherServlet 无法转换为 class jakarta.servlet.Servlet - java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet cannot be cast to class jakarta.servlet.Servlet Jboss7 EAP:UT010009:类型为org.springframework.web.servlet.DispatcherServlet类型的Servlet远程处理未实现javax.servlet.Servlet - Jboss7 EAP : UT010009: Servlet remoting of type class org.springframework.web.servlet.DispatcherServlet does not implement javax.servlet.Servlet 找不到org.springframework.web.servlet.DispatcherServlet - org.springframework.web.servlet.DispatcherServlet Not Found org.springframework.web.servlet.DispatcherServlet noHandlerFound - org.springframework.web.servlet.DispatcherServlet noHandlerFound CNF org.springframework.web.servlet.DispatcherServlet - CNF org.springframework.web.servlet.DispatcherServlet javax.servlet.ServletException: 错误实例化 servlet class org.springframework.web.servlet.DispatcherServlet - javax.servlet.ServletException: Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet 实例化servlet类org.springframework.web.servlet.DispatcherServlet时出错 - Error instantiating servlet class org.springframework.web.servlet.Dispatcherservlet Org.springframework.web.servlet.dispatcherservlet nohandler未找到映射 - Org.springframework.web.servlet.dispatcherservlet nohandler found no mapping found org.springframework.web.servlet.DispatcherServlet noHandlerFound:Spring MVC - org.springframework.web.servlet.DispatcherServlet noHandlerFound : Spring MVC 找不到类:org.springframework.web.servlet.DispatcherServlet - Class Not found : org.springframework.web.servlet.DispatcherServlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM