ShoppingCart 类型的clear() 方法未定义
我正在通过JSP实现一个购物网站。我有一个名为 ShoppingCart
的 Java 对象和一个名为 Item 的 Java 对象。在 ShoppingCart
中,有一个保存 Item 对象的向量。这个想法是,当我调用 addItem()
方法时,我使用:-
cart.addItem(name, image, price, details);
确保 ShoppingCart 已已经声明:-
ShoppingCart cart = new ShoppingCart("file_path_to_file");
以及 的内容>addItem
是:-
public void addItem(String name, String image, String price, String detail) throws IOException
{
items.add(new Item(name, image, price, detail));
this.saveMe();
}
其中 items 是向量。这绝对工作得很好。然而,我现在创建了一个名为clear的新方法:-
public void clear() throws IOException
{
items.clear();
this.saveMe();
}
saveMe方法只是保存到对象文件:-
private void saveMe() throws IOException
{
FileOutputStream fos = new FileOutputStream(this.filename);
ObjectOutputStream oos = new ObjectOutputStream(fos)
oos.writeObject(this.items);
oos.close();
}
当我使用以下方法调用clear方法时:-
cart.clear();
我收到错误消息:-
jsp文件/project/cart.jsp中第108行发生错误 ShoppingCart 类型的clear() 方法未定义
任何人都可以帮助我尝试解决此问题吗?
I am implementing a shopping website through JSP. I have a Java object called ShoppingCart
and one called Item. In ShoppingCart
there is a vector which holds Item objects. The idea is when I make a call to the addItem()
method, I use:-
cart.addItem(name, image, price, details);
ensuring that the ShoppingCart has already been declared:-
ShoppingCart cart = new ShoppingCart("file_path_to_file");
and the the content of addItem
is:-
public void addItem(String name, String image, String price, String detail) throws IOException
{
items.add(new Item(name, image, price, detail));
this.saveMe();
}
where items is the vector. This works absolutely fine. I have, however, now created a new method called clear:-
public void clear() throws IOException
{
items.clear();
this.saveMe();
}
The saveMe method simply saves to an Object file:-
private void saveMe() throws IOException
{
FileOutputStream fos = new FileOutputStream(this.filename);
ObjectOutputStream oos = new ObjectOutputStream(fos)
oos.writeObject(this.items);
oos.close();
}
When I call the clear method using:-
cart.clear();
I get the error message:-
An error occurred at line: 108 in the jsp file: /project/cart.jsp
The method clear() is undefined for the type ShoppingCart
Can anybody help with any ideas I can try to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果该方法是新添加的并且您收到此错误,则显然您仍在使用类路径中缺少该方法的旧版本的类。您需要保存源代码文件,重新编译/重建类/项目,重新部署项目并重新启动服务器以使新的更改生效。
顺便说一句,一个像样的 IDE 和一个像样的服务器插件会自动完成此操作。
If that method was newly added and you get this error, you're apparently still using an old version of the class lacking the method in the classpath. You need to save the source code file, recompile/rebuild the class/project, redeploy the project and restart the server to get the new changes to work.
A bit decent IDE with a bit decent server plugin will do that automagically by the way.
尝试(重新)编译代码,将其部署到服务器,然后打开 .jsp 页面。每次更改 java 代码(而不是 html 代码)时都执行此操作。
Java 不是 解释型语言,而是一种编译型编程语言,其程序在执行之前会转换为可执行形式。
Try (re)compiling the code, deploying it to the server and then opening your .jsp page. And do it every time you make changes in java code, not html code.
Java is not interpreted language, but a compiled programming language, whose programs are converted into an executable form before being executed.
我刚刚在 Eclipse 中遇到了一个非常相似的问题。我取消选择“项目->自动构建”选项,然后单击“项目->清理”并清理项目,但错误不会消失。最后,在单击“项目->清理”后弹出的对话框中,我取消选择“立即开始构建”选项并进行清理。
然后它最终“真正”清理了项目并清除了错误。然后我开始构建,一切都很好。
有一次,我注释掉了有问题的行并保存了,Eclipse 仍然报告该行上的错误!!! 我不知道这是 Eclipse 错误还是底层工具链错误,但它绝对不是在做 write 的事情。
这是使用 Eclipse Helios Service Release 2 和 Java SE 6 目标。在Windows XP下运行。
I was just fighting a very similar problem in Eclipse. I deselected "Project->Build Automatically" option and I was clicking "Project->Clean" and cleaning the project, but the error wouldn't go away. Finally, in the dialog that pops up after clicking "Project->Clean", I deselected the "Start a build immediately" option and did a clean.
It then FINALLY "really" cleaned the project and cleared the error. Then I started a build and all was fine.
At one point I had commented out the offending line and saved, and Eclipse was still reporting the error on that line!!! I don't know if this is an Eclipse bug or an underlying tool chain bug, but it's definitely not doing the write thing.
This was with Eclipse Helios Service Release 2 and a Java SE 6 target. Running under Windows XP.