java jsp“无法编译”和“无法解析为类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15985271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsp "unable to compile" and "cannot be resolved to a type"
提问by Bibodha
I have a jsp file that imports from my package folder inside my classes directory. When I try to use it, i get the "Unable to compile" and "cannot be resolved to a type" errors. Here are my code.
我有一个从我的类目录中的包文件夹导入的 jsp 文件。当我尝试使用它时,出现“无法编译”和“无法解析为类型”错误。这是我的代码。
<%@ page language="java" import="cs5530.*" %>
<html>
<head>
<script type="text/javascript">
function showInvalid()
{
document.getElementById("invalid").style.display = 'block';
document.getElementById("loginForm").reset();
}
</script>
</head>
<body>
<p>Welcome to FlixNet. Sign In</p>
<form name="loginForm" method="post" action="index.jsp">
User Name: <input type="text" name="username"><br>
Password: <input type="Password" name="password"><br>
<input type="submit" value="Submit">
<div id="invalid" style="display:none">
<p>Invalid username or password</p>
</div>
</form>
<%
Connector con = new Connector();
con.closeConnection();
%>
</body>
</html>
this is the error
这是错误
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 23 in the jsp file: /index.jsp
Connector cannot be resolved to a type
20: </div>
21: </form>
22: <%
23: Connector con = new Connector();
24: con.closeConnection();
25: %>
26: </body>
An error occurred at line: 23 in the jsp file: /index.jsp
Connector cannot be resolved to a type
20: </div>
21: </form>
22: <%
23: Connector con = new Connector();
24: con.closeConnection();
25: %>
26: </body>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
this is my file structure
这是我的文件结构
public_html
index.jsp
|WEB-INF
|classes
mysql.jar
|cs5530
Connector.java
Connector.class
other java and class files.
my Connector class is under the package cs5530 so that isn't the issue. The connector class is a simple db connector class that I have tested and it works.
我的连接器类在 cs5530 包下,所以这不是问题。连接器类是一个简单的数据库连接器类,我已经测试过它并且可以工作。
回答by Gaurav Sharma
copy the jar in WEB-INF/lib/
directory and then restart your web container
将 jar 复制到WEB-INF/lib/
目录中,然后重新启动 Web 容器
回答by Rob Clennell
This is because the xxx.jar file that describes the package you're trying to import has to be directly in the lib folder. e.g.: WebContent/WEB-INF/lib/xxx.jar. If it can only find the package folder inside the /lib then even though your .jar is inside this package folder that is still enough to create the error that it can only resolve to a 'package' and cannot resolve to a 'type'.
这是因为描述您尝试导入的包的 xxx.jar 文件必须直接位于 lib 文件夹中。例如:WebContent/WEB-INF/lib/xxx.jar。如果它只能在 /lib 中找到包文件夹,那么即使您的 .jar 在此包文件夹中,它仍然足以创建错误,即它只能解析为“包”而无法解析为“类型”。
回答by J.M.I. MADISON
You are missing importsat top of your .jsp file:
您缺少.jsp 文件顶部的导入:
<%@ page import="packageWhatever.packageWhatever.ClassName" %>
<%@ page import="packageWhatever.packageWhatever.ClassName" %>
Simplified example of problem:
问题的简化示例:
JSP FILE:
JSP文件:
TOMCAT_PRJ_DIR\src\main\webapp\index.jsp
TOMCAT_PRJ_DIR\src\main\webapp\index.jsp
WRONG:
错误的:
<html><body><h1>
<%= Main.TEST_FUNCTION() %>
</h1></body></html>
CORRECT:
正确的:
<%@ page import="launch.Main" %>
<html><body><h1>
<%= Main.TEST_FUNCTION() %>
</h1></body></html>
FILE BEING IMPORTED:
正在导入的文件:
TOMCAT_PRJ_DIR\src\main\java\launch\Main.java
TOMCAT_PRJ_DIR\src\main\java\launch\Main.java
package launch;
.... imports here ....
public class Main {
public static final String
TEST_FUNCTION(){
return( "[TEST_FUNCTION]" );
};
...other code...
};
Stack Used: Java+Tomcat+Maven
使用的堆栈:Java+Tomcat+Maven
回答by UVM
Please check whether Connector.class
is really imported. It looks like that you have not imported it.
请检查是否Connector.class
真的进口。看起来您还没有导入它。
回答by JRR
Try
尝试
<%@ page import="cs5530.*" %>
回答by Bibodha
This was a result of me not being in charge of my Tomcat server. Apparently it needs to be cleaned if something major changes. My code was spotless.
这是因为我没有负责我的 Tomcat 服务器。显然,如果发生重大变化,则需要对其进行清洁。我的代码一尘不染。