为servlet分配异常 - JAVA中的Web编程

时间:2017-08-02 06:37:56

标签: java spring apache jsp tomcat

我对JAVA很新,但我在网络编程方面有一些经验。我正在创建简单的jsp页面并使用@RequestMapping进行页面查看。但是我面临一个例外,我已经用Google搜索了但没有找到解决我问题的商品。 以下是完整堆栈跟踪的例外:

  

严重:为servlet分配异常[WebAppGraph]   java.lang.ClassNotFoundException:com.WebAppGraph.app at   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)     在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)     在   org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:508)     在   org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:489)     在   org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1041)     在   org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:770)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)     在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)     在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:796)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1372)     在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:745)

我的代码示例:

indexController.java

package com.WebAppGraph.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class indexController {

    @RequestMapping("/")
    public String home() {
        return "index.jsp";
    }

    @RequestMapping("/test.html")
    public String test() {
        return "test.jsp";
    }

}

Web.xml中

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>WebAppGraph</servlet-name>
        <servlet-class>
            com.WebAppGraph.app
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebAppGraph</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

WebAppGraph-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

</beans>

的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.WebAppGraph.app</groupId>
    <artifactId>WebAppGraph</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>WebAppGraph Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>WebAppGraph</finalName>
    </build>
</project>

文件结构: enter image description here

当我尝试运行我的程序时,会打开index.jsp页面,但是当我写localhost:9000/WebAppGraph/test.jsp时,第二页才会打开但是当我写localhost:9000/WebAppGraph/test.html时它没有显示给我test.jsp,而是显示我上面发布的异常。 知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:3)

对于servlet映射,您可以在web.xml中执行类似的操作:

<servlet>
    <servlet-name>WebAppGraph</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/WebAppGraph-servlet.xml</param-value>
    </init-param>
</servlet>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
    <servlet-name>WebAppGraph</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>/</url-pattern>
</servlet-mapping>

答案 1 :(得分:2)

你需要尝试一个spring-mvc hello world教程。 您的班级名称为com.WebAppGraph.app.indexController而不是com.WebAppGraph.app,如果您要将其列入web.xml 需要扩展<Servlet>的{​​{1}}标记。你所拥有的是一个注释为spring MVC控制器而不是Servlet的spring bean。您希望将spring MVC dispatch servlet放在XML中:

javax.servlet.Servlet

然后在spring xml(你已经在做)中使用上下文扫描注入Controller注释

答案 2 :(得分:1)

更改servlet-class文件中web.xml标记的值。您可以更改如下(因为我们大多数人使用DispatcherServlet作为servlet-class):

<servlet>
    <servlet-name>WebAppGraph</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

另外一件事,如果你使用spring的默认配置,那么你需要从返回的控制器函数字符串中删除.jsp,如下所示:

@RequestMapping("/")
public String home() {
    return "index";
}

@RequestMapping("/test.html")
public String test() {
    return "test";
}