我有几个使用 servlet 的 J2EE 项目,每个项目都从处理主要 doGet 和 doPost 方法的基类扩展,并在子类上调用更具体的方法。我不想将该基类作为每个 WAR 文件中的类,而是希望将其移动到其他每个项目都可以引用的自己的 JAR 中。
我正在使用 Eclipse。因此,我创建了一个新的 Java 项目,并将基类移至其中。它提示没有引用 servlet-api.jar,所以我将其添加到那里。然后其他项目提示没有对该新项目的引用,因此添加了这些引用。
现在,不再有提示了,但是当我运行该项目时(通过打开 servlet 类并选择运行方式),它不会从其他项目中导出类,并且 Tomcat 会抛出一个错误尝试实例化未知类的子类时出现异常。
如何告诉 eclipse 导出 servlet 项目本身及其引用的其他 java 项目?
最佳答案
I'm using Eclipse. So, I created a new Java project, and moved the base class into there. It complained about not having a reference to servlet-api.jar, so I added it there.
确保您通过Java构建路径添加它 > 库> 添加库> 服务器运行时> < em>[选择有问题的服务器](因此不复制或导入,否则类路径可能与实际使用的服务器的类路径冲突)。
Then the other projects complained about not having a reference to that new project, so those references were added.
我不确定我是否理解这里的根本原因。
Now, there's no more complaining, but when I run the project (by opening the servlet class and choosing Run As), it doesn't export the classes from the other project and Tomcat throws an exception when trying to instantiate a subclass of an unknown class.
您还需要配置已部署的 Web 项目以导出项目引用。转到项目属性中的“Java EE 模块依赖项”,然后勾选要作为依赖项导出的项目。
关于eclipse - 开发 J2EE Web 应用程序时如何引用和导出另一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2298130/