Spring 4中出错| java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet

时间:2014-07-04 20:19:40

标签: java spring maven spring-mvc

我在执行spring 4应用程序时遇到错误,你能帮我吗?

  

抛出java.lang.ClassNotFoundException:   org.springframework.web.servlet.DispatcherServlet at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177)     在   org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:550)     在   org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:531)     在   org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4932)     在   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5218)     在   org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1396)     在   org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1386)     在java.util.concurrent.FutureTask.run(未知来源)at   java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)at   java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)at   java.lang.Thread.run(未知来源)

这是我的pom.xml

<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.anandc.webmvc</groupId>
    <artifactId>MVC_Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Spring4MVCWebApp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.0.5.RELEASE</spring.version>
        <freemarker.version>2.3.20</freemarker.version>
        <spring.security.version>3.2.3.RELEASE</spring.security.version>
        <spring.security.taglib.version>3.0.5.RELEASE</spring.security.taglib.version>
        <spring.jdbc.version>4.0.2.RELEASE</spring.jdbc.version>
        <common.logging.version>1.1.1</common.logging.version>
        <servlet.api.version>3.0.1</servlet.api.version>
        <jstl.version>1.2</jstl.version>        
    </properties>

    <dependencies>

        <!-- JUnit dependencies Start -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- JUnit dependencies End -->

        <!-- FTL dependencies Start -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>${freemarker.version}</version>
        </dependency>
        <!-- FTL dependencies End -->

        <!-- Spring dependencies Start -->
        <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-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring dependencies End -->
        <!-- Spring Security Artifacts - START -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.taglib.version}</version>
        </dependency>
        <!-- Spring Security Artifacts - END -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${common.logging.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.jdbc.version}</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <finalName>Spring4MVCWebApp</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  

这就是我的web.xml的样子

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">

  <display-name>Freemarker_SpringMVC_example</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>
  

这就是我的spring-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- freemarker config -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
    </bean>

    <!--
      View resolvers can also be configured with ResourceBundles or XML files. If you need
      different view resolving based on Locale, you have to use the resource bundle resolver.
    -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="cache" value="true"/>
      <property name="prefix" value=""/>
      <property name="suffix" value=".ftl"/>
    </bean>

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

</beans>

另外,当我使用Spring 4时,请建议我是否需要在POM中进行任何清理。

提前谢谢!

请注意 - 我已经完成了现有的类似问题,但这些都没有帮助。

的问候,
Anand Chavan

2 个答案:

答案 0 :(得分:1)

您可以尝试:
1,更改spring-servlet.xml:

webView.getView().post(new Runnable(){
  public void run(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      webView.sendJavascript(js);
    } else {
      webView.loadUrl("javascript:" + js);
    }
  }
});

2,如果您将jar放在 Libraries 上,则尝试将这些jar放到 WEB-INF / lib

我通过上述方法解决了这个问题 但我真的不知道为什么。

答案 1 :(得分:0)

尝试下面的技巧,每次都适合我:

  1. 选择项目并转到属性&gt;部署程序集
  2. 点击添加
  3. 选择 Java构建路径条目
  4. 如果存在任何Maven依赖项,请选择全部(Ctrl + A)并单击完成
  5. 清理并刷新项目
  6. 发布并尝试立即启动您的服务器,

    希望这个帮助, 最诚挚的问候,