无法解决部署 JSP 期间的符号错误
我刚刚学习了基本的 Servlet 和 JSP 技术,并正在使用它设计一个简单的网站。
该网站的目的是销售产品。产品的详细信息存储在数据库中。我想从数据库中检索数据并显示动态页面。我正在使用 MVC 方法并尝试使其尽可能面向对象。
我面临类别页面的问题(该页面旨在充当各种产品的索引......我想检索存储在数据库中的类别并显示它们)。
详细信息如下:
我创建了一个简单的 java 类来表示该表。该表在数据库中的名称为“Categories”...该类名为 CategoryTable,包含表示表的各种属性的实例字段。
名为 CategoryRetriever 的 POJO 充当我的模型。它将表中特定行的数据插入到 CategoryTable 的对象中,最后创建各个 CategoryTable 对象的 ArrayList。此 ArrayList 包含所有检索到的数据。
设计的控制器是一个名为 CategoryController 的 Servlet。它创建了 CategoryRetriever 对象,并将其传递给名为 CategoryDisplayer 的 JSP。
所有的事情都编译得很好。 JSP 除外。用WEBLOGIC部署后。 jsp给出以下错误。
G:\bea\weblogic81\server\bin.\myserver.wlnotdelete\extract\myserver_MiniProject_build\jsp_servlet__categorydisplayer.java:173:无法解析符号符号:类 Category表位置:class jsp_servlet.__categorydisplayer CategoryTable tp = (CategoryTable)categoryContent.get(i); //[ /CategoryDisplayer.jsp;线路:35] ^
由此我可以推断,直接位于根项目目录下的JSP找不到root>>WEB-INF>>源中的CategoryTable类。
我的 JSP 是否需要包含声明或其他内容?如果是,该怎么做?
I just learnt basic Servlets and JSP technology and am designing a simple website using it.
The aim of the website is to sell products. The details of the products are stored in the database. I want to retrieve the data from the database and display the dynamic pages. I am using the MVC approach and trying to make it as OO as i can.
I am facing problem with the category page (which is intended to act as an index for the various products...i want to retrieve the categories stored in the DB and display them).
The details are as follows:
I have created a simple java class which represents the table. The table's name is "Categories" in the DB...This class is named CategoryTable and contains instance fields representing various attributes of the table.
A POJO named CategoryRetriever acts as my Model. it inserts the data of a particular row from the table into an object of CategoryTable and finally creates an ArrayList of the various CategoryTable Objects. This ArrayList contains all the retrieved data.
The Controller of the Design is a Servlet names CategoryController. it creates and object of the CategoryRetriever and passes this to a JSP named CategoryDisplayer.
All the things are getting compiled fine. Except the JSP. After deploying with WEBLOGIC. the jsp gives the following error.
G:\bea\weblogic81\server\bin.\myserver.wlnotdelete\extract\myserver_MiniProject_build\jsp_servlet__categorydisplayer.java:173: cannot resolve symbol symbol : class
CategoryTable location: class
jsp_servlet.__categorydisplayer
CategoryTable tp = (CategoryTable)categoryContent.get(i);
//[ /CategoryDisplayer.jsp; Line: 35]
^
From this what I can infer is that the JSP which is directly under the root project directory cannot find the CategoryTable class which is inside root>>WEB-INF>>source.
Does my JSP require an include statement or something? If yes, how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,这是因为你没有将类放在包中。无包类对于包内的其他类不可见/不可导入。 JSP 文件被编译并转换为扩展 JspServlet 的类,该类被放置在 servlet 容器特定的包中。从那里它无法查看/导入无包类。
每当您希望能够在其他地方重用/导入类时,始终将其放入包中。
例如,
请注意,无包 servlet 将在特定配置(例如 Apache Tomcat)中的特定版本的特定 servlet 容器上工作,但这不是避免将类放入包中的有效执行。
也就是说,这表明您正在 JSP 文件中使用 scriptlet。这是一个不好的做法。而是使用 servlet 为 JSP 准备数据,使用 JSTL 等标记库来控制 JSP 页面中的流程,并使用 EL(表达式语言,那些
${}
东西)来访问后端数据。另请参阅
更新:既然你提到使用记事本/cmd,我只想强调Saheed more 的回答:您不应该将类(
*.class
)文件与源(*.java
)文件放在同一文件夹中,而应该放在/WEB中-INF/类
。使用上述包示例时,类应放置在以下位置:/WEB-INF/classes/com/shahensha/model/Category.class
/WEB-INF/classes/ com/shahensha/dao/CategoryDAO.class
/WEB-INF/classes/com/shahensha/controller/CategoryController.class
如果您使用的是 IDE,它将负责编译和自动构建。
At first sight, this is because you didn't put the class in a package. Packageless classes are not visible/importable from other classes inside a package on. JSP files are namely compiled and converted to a class extending
JspServlet
which is been placed in a servletcontainer-specific package. From there it cannot see/import packageless classes.Whenever you want to be able to reuse/import a class somewhere else, always put it in a package.
E.g.
Note that packageless servlets will work on specific servletcontainers of specific versions in specific configurations (e.g. Apache Tomcat), but this isn't a valid execuse to refrain from placing classes in a package.
That said, this indicates that you're using scriptlets inside a JSP file. This is a bad practice. Rather use a servlet to prepare the data for JSP, use taglibs like JSTL to control flow in JSP page and use EL (Expression Language, those
${}
things) to access backend data.See also
Update: since you mentioned to use notepad/cmd, I would only emphasize the answer of Saheed more: you should not keep class (
*.class
) files in the same folder as source (*.java
) files, but in/WEB-INF/classes
. When using the above package examples, the classes should be placed in the following locations:/WEB-INF/classes/com/shahensha/model/Category.class
/WEB-INF/classes/com/shahensha/dao/CategoryDAO.class
/WEB-INF/classes/com/shahensha/controller/CategoryController.class
If you were using an IDE, it will take care about compiling and building automagically.
...找不到 root>>WEB-INF>>source 中的 CategoryTable 类...
我认为你的意思是 WEB-INF/classes 文件夹。
我的 JSP 是否需要包含语句或其他内容...如果需要,该怎么做?
是的。要导入丢失的类:
... cannot find the CategoryTable class which is inside root>>WEB-INF>>source...
I think you mean the WEB-INF/classes folder.
Does my JSP require an include statement or something...if yes, how to do it?
Yes. To import your missing class: