Java/JSP WEB-INF/classes 无法导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1700416/
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
Java/JSP WEB-INF/classes can't import
提问by Pierre
Its been a while since I had to do some Java/JSP...
自从我不得不做一些 Java/JSP 以来已经有一段时间了......
I have a java class in WEB-INF/classes/MyClass.java The build in Netbeans is successful and I can see MyClass.class in the classes folder.
我在 WEB-INF/classes/MyClass.java 中有一个 java 类 Netbeans 中的构建成功,我可以在 classes 文件夹中看到 MyClass.class。
In my jsp page, I've got
在我的jsp页面中,我有
<%@ page import="MyClass" %>
Tomcat says that the import cannot be resolved...
Tomcat 说无法解析导入...
I tried to put MyClass in a package(WEB-INF/com/MyClass) and then import the package into my Jsp file. The import doesnt throw an error anymore then, but I cannot instanciate a MyClass object, says the type is unresolved...
我试图将 MyClass 放在一个包中(WEB-INF/com/MyClass),然后将该包导入到我的 Jsp 文件中。导入不再抛出错误,但我无法实例化 MyClass 对象,说类型未解析......
What am I doing wrong here..?
我在这里做错了什么..?
All help appreciated! :)
感谢所有帮助!:)
采纳答案by Pierre
OMG, I found my mistake...
天哪,我发现我的错误......
Netbeans wasn't copying the lib files to the right folder, my jsp page was being updated, so it looked like all the files were copied, but actually MyClass.class wasnt in the folder...
Netbeans 没有将 lib 文件复制到正确的文件夹,我的 jsp 页面正在更新,所以看起来所有文件都被复制了,但实际上 MyClass.class 不在文件夹中......
Thanks for your help!
谢谢你的帮助!
回答by KB22
WEB-INF/classes/MyClass.java
makes me guess that you're using the default package which is'nt a good practice at all. Try assigning your class to a package and do the import according to that.
让我猜你使用的是默认包,这根本不是一个好习惯。尝试将您的班级分配给一个包并根据该包进行导入。
Do something like:
做类似的事情:
package myPackage;
class myClass
{
...
}
And afterwards:
之后:
<%@ page import="myPackage.myClass" %>
回答by adatapost
.class file must be placed inside the classesfolder under WEB-INF. So, the location of MyClass.class must be WEB-INF/classes/com/ (In case comis a package).
.class 文件必须放在WEB-INF下的classes文件夹中。所以,MyClass.class 的位置必须是 WEB-INF/classes/com/(如果com是一个包)。
<%
// Instantiate a MyClass
com.MyClass obj=new com.MyClass();
%>
OR
或者
<%@ page import="com.MyClass" %>
<%
MyClass obj=new MyClass();
%>
回答by djna
What package is MyClass in? If the default package then you can put the class file in
MyClass 在什么包中?如果是默认包那么你可以把类文件放在
WEB-INF/classes
if it's in a package, then use the package directory hierarchy under classes
如果它在包中,则使用类下的包目录层次结构

