我正在为初学者开设 Udemy、JSP、Servlet 和 JDBC 类(class):构建数据库应用程序,并在使用 Tomcat 9.0.27、XAMPP 和 Eclipse 时遇到此问题。我不确定错误是什么或错误在哪里,并且我已经按照指示完成了自定义包:
Java包+Java类文件
package com.jspdemo.jsp;
public class FunUtils {
public static String makeItLower(String data) {
return data.toLowerCase();
}
}
JSP 文件
<%@ page import="com.jspdemo.jsp.*" %>
<html>
<body>
Let's have some fun: <%= FunUtils.makeItLower("FUN FUN FUN") %>
</body>
</html>
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [7] in the jsp file: [/fun-test.jsp] FunUtils cannot be resolved 4: 5: 6: 7: Let's have some fun: <%= FunUtils.makeItLower("FUN FUN FUN") %> 8: 9: 10:
Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:519) org.apache.jasper.compiler.Compiler.compile(Compiler.java:392) org.apache.jasper.compiler.Compiler.compile(Compiler.java:362) org.apache.jasper.compiler.Compiler.compile(Compiler.java:346) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Note The full stack trace of the root cause is available in the server logs.
之前的JSP实践都很顺利(JSP Hello World,JSP 表达式、JSP Scriptlet、JSP 声明测试)直到我不得不 在 Java 包中创建 Java 类并需要导入它 进入我的 JSP 文件。
为什么第7行有错误?我认为如果我有的话它就连接了 在项目文件夹中创建了一个新包和一个新的 Java 类 本身?我什至将 Java 类导入到 JSP 文件中,如下所示 <%@ page import="com.jspdemo.jsp.*"%>。我到底错过了什么,因为教练能够完成它。另外,我以后该如何防止这种情况发生? 谢谢!
编辑:顺便说一句,我在 10 个月前看到过类似的情况 Unable to compile class for JSP:Tomcat 9我尝试重新启动 XAMPP、Apache-Tomcat、Eclipse,但所有这些都不起作用。我怀疑类路径是错误的,因为我也一步一步地遵循了它。
编辑2:我什至像链接中的人一样重新安装了JDK和Eclipse,他显然已经解决了问题,但对我来说仍然没有好处。 JDK 13.0.1 和 Eclipse 2019-093。
最佳答案
没关系,伙计们,显然教程跳过了一个步骤或其他内容。显然,我必须先“构建”“项目”,然后才能运行它,并且它会按预期显示其输出。问题解决了!
关于java - 无法在 Eclipse 中编译 JSP 类。 Apache-Tomcat 9.0.27,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097047/