java.lang.InstantiationException:在作用域内找不到bean [name] [英] java.lang.InstantiationException: bean [name] not found within scope

查看:316
本文介绍了java.lang.InstantiationException:在作用域内找不到bean [name]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用jsp和bean,并且无法理解我遇到的问题。

I am learning how to use jsp and beans now, and can't understand what the issue I'm having.

我正在尝试创建一个类似bean的bean这个:

...

I am trying to create a bean like this: ...

我收到错误:

java.lang.InstantiationException:在范围内找不到bean预留

java.lang.InstantiationException: bean reservation not found within scope

我在网上看过,大多数人似乎建议使用class = ...而不是type =...,或使用import语句。我已经在做前者并尝试了后者...任何想法?

I have looked around online, and most people seem to recommend using class="..." instead of type="...", or using an import statement. I am already doing the former and tried the latter...any ideas?

这是豆子:

package homework10;

public class Reservation {

private int groupSize;
private String status;
private double cost;
private boolean triedAndFailed;

public Reservation(){      
}

public void setGroupSize(int gs)
{
    groupSize = gs;
}

public int getGroupSize()
{
    return groupSize;
}
public void setStatus(String str)
{
    this.status = str;
}

public String getStatus()
{
    return status;
}    

public void setCost(double cost)
{
    this.cost = cost;
}

public double getCost()
{
    return cost;
} 

public void setTriedAndFailed(boolean bool)
{
    this.triedAndFailed = bool;
}

public boolean isTriedAndFailed()
{
    return triedAndFailed;
}    

}

这是jsp页面的开头:

and this is the beginning of the jsp page:

<head>
    <!--<script type="text/javascript" src="functions8.js">
    </script>-->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BHC Calculator-HTML-validation + Servlet Version</title>
    <jsp:useBean id = "reservation" class = "homework10.Reservation" scope = "session" />
</head>

提前致谢!

推荐答案


java.lang.InstantiationException

java.lang.InstantiationException

这基本上意味着以普通的Java术语表示以下构造

This basically means in plain vanilla Java terms that the following construct

import homework10.Reservation;

// ...

Reservation reservation = new Reservation(); 

失败。

有很多可能的原因:


  1. 运行时类路径中缺少类。

  2. 无法找到类定义。

  3. 类不公开。

  4. 类没有公共默认构造函数。

  5. 代码在公共默认构造函数中抛出异常。

  1. Class is missing in runtime classpath.
  2. Class definition cannot be found.
  3. Class is not public.
  4. Class does not have a public default constructor.
  5. The code in the public default constructor threw an exception.

根据目前提供的代码,并假设您100%确定你正在运行你认为正在运行的代码,那只能是#1或#2。该类是公共的,并且有一个公共默认构造函数,它基本上什么也没做。因此可以划分#3,#4和#5。

Based on the code provided so far, and assuming that you're 100% certain that you're running the code you think you're running, then that can only be cause #1 or #2. That class is public and has a public default constructor which does essentially nothing. So #3, #4 and #5 can be scratched.

要修复可能的原因#1,请确保路径中的webapp部署中存在类文件

/WEB-INF/classes/homework10/Reservation.class 。为了解决可能的原因#2,您还应该确保在保留包结构的同时以正确的方式编译类。因此,当你没有使用像Eclipse这样的IDE,但是你在命令提示符下摆弄低级别时,那么你应该确保在编译类时包含该包。

To fix possible cause #1, make sure that the class file is present in the webapp deploy in the path
/WEB-INF/classes/homework10/Reservation.class. To fix possible cause #2, you should also make sure that the class is compiled the right way while preserving the package structure. So, when you're not using an IDE like Eclipse, but you're fiddling low level in command prompt, then you should make sure that you include the package while compiling the class.

关于您找到的可能解决方案,

As to the possible solutions you found,


和大多数人们似乎建议使用class =...而不是type =...

这是正确的。要了解更多信息,请转到以下答案: javax .servlet.ServletException:在范围内找不到bean [name]但是,这显然不是你特定情况下的原因,因为它显然没有解决问题。

That's correct. To learn more, head to this answer: javax.servlet.ServletException: bean [name] not found within scope However, this is clearly not the cause in your particular case as it apparently didn't solve the problem.


或使用import语句

这没有完全没有感。那些人对 scriptlet 感到困惑。他们应该在所有程度上避免。实际上,< jsp:useBean> 也是如此,但这是一个不同的故事。另请参阅我们的Servlets维基页面以获取一些提示。

This makes no utter sense. Those people are confusing with scriptlets. They should be avoided to all extent. The <jsp:useBean> also, actually, but that's a different story. See also our Servlets wiki page for some hints.

这篇关于java.lang.InstantiationException:在作用域内找不到bean [name]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆