简体   繁体   English

Tomcat找不到直接放在classes文件夹下的类

[英]Tomcat can't find class that is placed directly under classes folder

I have the following JSP: 我有以下JSP:

<%@ page import="foo.*" %>
<html>
    <body>
        The page count is:
        <%=Counter.getCount()%>
    </body>
    </html>

I have a Counter class in package foo which is stored in: 我在包foo有一个Counter类,它存储在:

C:\\apache-tomcat-6.0.32\\webapps\\God\\WEB-INF\\classes C:\\ Apache的Tomcat的6.0.32 \\的webapps \\神\\ WEB-INF \\班

And the container could find the class from its package foo . 容器可以从它的包foo找到类。

But when I try to add some other class file directly under \\WEB-INF\\classes and not in any specific package such as foo , then container can't find that class. 但是当我尝试直接在\\WEB-INF\\classes下添加一些其他类文件而不是在foo等任何特定包中添加时,容器找不到该类。

How is this caused and how can I solve it? 这是怎么造成的,我该如何解决?

Classes in the default package are not visible to classes which are by itself inside a package. 默认包中的类对于包本身内的类是不可见的。 You must put the class in a package whenever you want to import it in another class which is by itself inside a package. 每当要将类导入到另一个类中时, 必须将该类放在一个包中,该类本身就在一个包中。 Technically, when JSP files are compiled, the container will autogenerate a .class file which is by itself inside a package. 从技术上讲,当编译JSP文件时,容器将自动生成一个.class文件,该文件本身就在一个包中。 So you cannot import classes from the default package in the JSP. 因此,您无法从JSP中的默认包中导入类。

So, whenever you want to be able to reuse a class anywhere, it has to be placed in a concrete package, not in the default package. 因此,只要您希望能够在任何地方重用类,就必须将其放在具体的包中,而不是放在默认包中。 As an exercise, create two classes yourself, one which is inside a package and other which is not inside a package. 作为练习,自己创建两个类,一个在包内,另一个不在包内。 Now, inside the one with package, try to import and use the one without package. 现在,在带有包的那个内部,尝试import并使用没有包的那个。 You'll see that it's not possible and the code won't compile. 你会发现它是不可能的,代码也不会编译。 The servletcontainer encounters exactly the same problem "under the hoods". servletcontainer在“引擎盖下”遇到完全相同的问题。

See also: 也可以看看:


Unrelated to the concrete problem: writing raw Java code in JSP files is a poor practice . 具体问题无关 :在JSP文件中编写原始Java代码是一种不好的做法 Consider learning and using servlets . 考虑学习和使用servlet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tomcat 在 OpenJDK 11 上找不到类 - Tomcat can't find classes on OpenJDK 11 我直接在src文件夹中添加了一个库,Eclipse似乎可以正确编译,但是找不到类文件? - I added a library directly to my src folder, Eclipse seems to be compiling properly, but can't find the class file? Eclipse在我的/ test源文件夹中的类路径上找不到类 - Eclipse can't find classes on the classpath in my /test source folder 我可以在Tomcat上将相同Java包的类文件拆分为jar文件和classes文件夹吗? - Can I split class files of same java package into jar file and classes folder on Tomcat? Tomcat找不到类freemarker.template.TemplateException - Tomcat can't find a class freemarker.template.TemplateException 共享/类和战争文件下的Jave类-在Tomcat中加载的优先级 - Jave Class under shared/classes and war file - priority of loading in tomcat 在svn下的eclipse中找不到或编辑Java类 - can't find or edit java class in eclipse under svn 用IntelliJ的想法,如何配置使资源/文件夹下的文件编译到classes / config /文件夹,而不是直接在classes /文件夹下 - In intellij idea, how config to make the files under the resource/ folder compiler to classes/config/ folder, not directly under classses/ folder 为什么WEB-INF文件夹里面的jsp文件有效,但放在WEB-INF下的文件夹下不行? - Why jsp files inside WEB-INF folder works , but placed under a folder under WEB-INF doesn't? Tomcat7在WEB-INF / lib / lookHere.jar中找不到类 - Tomcat7 can't find classes in WEB-INF/lib/lookHere.jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM