java.lang.InstantiationException: bean [name] 在范围内未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17842892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:11:57  来源:igfitidea点击:

java.lang.InstantiationException: bean [name] not found within scope

javajspjavabeans

提问by Jo.P

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

我现在正在学习如何使用 jsp 和 bean,但不明白我遇到了什么问题。

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

我正在尝试创建一个这样的 bean:...

and am getting the error:

并收到错误:

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

java.lang.InstantiationException:在范围内未找到 bean 保留

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?

我在网上环顾四周,大多数人似乎建议使用 class="..." 而不是 type="...",或者使用 import 语句。我已经在做前者并尝试了后者......有什么想法吗?

This is the bean:

这是豆子:

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;
}    

}

}

and this is the beginning of the jsp page:

这是jsp页面的开头:

<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>

Thanks in advance!

提前致谢!

回答by BalusC

java.lang.InstantiationException

java.lang.InstantiationException

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

这基本上意味着在普通的 Java 术语中,以下构造

import homework10.Reservation;

// ...

Reservation reservation = new Reservation(); 

has failed.

失败了。

There are many possible causes for this:

造成这种情况的可能原因有很多:

  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.
  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.

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

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.

要修复可能的原因 #1,请确保该类文件存在于 webapp 部署的路径中
/WEB-INF/classes/homework10/Reservation.class。要解决可能的原因 #2,您还应该确保以正确的方式编译类,同时保留包结构。因此,当您不使用像 Eclipse 这样的 IDE,但您在命令提示符中摆弄低级别时,那么您应该确保在编译类时包含该包。



As to the possible solutions you found,

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

and most people seem to recommend using class="..." instead of type="..."

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

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

没错。要了解更多信息,请前往以下答案:javax.servlet.ServletException: bean [name] not foundin scope但是,这显然不是您的特定情况下的原因,因为它显然没有解决问题。

or using an import statement

或使用导入语句

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

这完全没有意义。那些人对scriptlet感到困惑。应该尽量避免它们。该<jsp:useBean>还,实际上,但是这是一个不同的故事。另请参阅我们的 Servlets wiki 页面以获取一些提示。