javax.servlet.ServletException:在范围内找不到 bean [名称]

发布于 2024-07-08 16:11:35 字数 330 浏览 14 评论 0原文

我收到此错误:

javax.servlet.ServletException: bean not found within scope

在顶部有此错误的页面上。

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

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

这是如何引起的以及如何解决?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-07-15 16:11:35

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

以下:

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

在幕后基本上执行以下操作:

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

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

// Use bean ...

而以下:

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

在幕后基本上执行以下操作:

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

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

// Use bean ...

如果它之前有效并且“突然”不起作用,那么这意味着负责将 bean 放入作用域的某物已停止工作。 例如,一个 Servlet 在 doGet() 中执行以下操作:

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

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

You need the class attribute instead of the type attribute.

The following:

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

does basically the 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 something which is responsible for putting the bean in the scope has stopped working. For example a servlet which does the following in the 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-INF and forward to it instead.

∞梦里开花 2024-07-15 16:11:35

您必须添加

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

因为默认情况下,bean 是页面范围

You must add

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

Because by default the bean is looked on the page scope

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文