javax.servlet.ServletException: bean [name] 在范围内未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/270444/
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-08-11 12:18:19  来源:igfitidea点击:

javax.servlet.ServletException: bean [name] not found within scope

javajspusebean

提问by stu

I'm getting this error:

我收到此错误:

javax.servlet.ServletException: bean not found within scope

on a page with this at the top.

在顶部有这个的页面上。

<jsp:useBean id="bean" type="com.example.Bean" scope="request" />

The class exists in the classpath, it worked this morning, and I don't get what not found within scope means.

该类存在于类路径中,今天早上它起作用了,我不明白在范围内找不到什么意思。

How is this caused and how can I solve it?

这是怎么引起的,我该如何解决?

采纳答案by BalusC

You need the classattribute instead of the typeattribute.

您需要class属性而不是type属性。

The following:

下列:

<jsp:useBean id="bean" type="com.example.Bean" scope="request" />

does basicallythe following behind the scenes:

确实基本上幕后如下:

Bean bean = (Bean) pageContext.getAttribute("bean", PageContext.REQUEST_SCOPE);

if (bean == null) {
    throw new ServletException("bean not found within scope");
}

// Use bean ...

While the following:

而以下:

<jsp:useBean id="bean" class="com.example.Bean" scope="request" />

does basically the following behind the scenes:

在幕后基本上做了以下工作:

Bean bean = (Bean) pageContext.getAttribute("bean", PageContext.REQUEST_SCOPE);

if (bean == null) {
    bean = new Bean();
    pageContext.setAttribute("bean", bean, PageContext.REQUEST_SCOPE);
}

// Use bean ...

If it has worked before and it didn't work "in a sudden", then it means that somethingwhich is responsible for putting the bean in the scope has stopped working. For example a servlet which does the following in the doGet():

如果以前的工作,并没有“在突然”的工作,那么就意味着事情是负责把豆的范围已停止工作。例如一个 servlet,它在以下内容中执行以下操作doGet()

request.setAttribute("bean", new Bean());
request.getRequestDispatcher("page.jsp").forward(request, response);

Maybe you've invoked the JSP page directly by URL instead of invoking the Servlet by URL. If you'd like to disable direct access to JSP pages, then put them in /WEB-INFand forward to it instead.

也许你是直接通过 URL 调用 JSP 页面,而不是通过 URL 调用 Servlet。如果您想禁用对 JSP 页面的直接访问,请将它们放入/WEB-INF并转发给它。

回答by victor hugo

You must add

你必须添加

<jsp:useBean id="givingFormBean" type="some.packg.GivingForm" scope="request" />

Because by default the bean is looked on the pagescope

因为默认情况下,bean页面范围内查看