gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:34:25 24 4
gpt4 key购买 nike

我对 JAVA 很陌生,但我在网络编程方面有一些经验。我正在创建简单的 jsp 页面并使用 @RequestMapping 进行页面浏览。但我遇到了一个异常(exception),我用谷歌搜索但没有找到解决我问题的商品。这是完整堆栈跟踪的异常:

SEVERE: Allocate exception for servlet [WebAppGraph] java.lang.ClassNotFoundException: com.WebAppGraph.app at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:508) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:489) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1041) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:770) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at 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,而是向我显示我在上面发布的异常。知道如何解决这个问题吗?

最佳答案

对于您的 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>

关于java - 为 servlet 分配异常 - JAVA 中的 Web 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com