我收到了这个错误:

javax.servlet.ServletException: bean not found within scope
在页面上的

位于顶部。

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

该类存在于类路径中,它今天早上起作用,而且我没有得到范围内未找到的内容。

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

有帮助吗?

解决方案

您需要 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 中并转发给它。

其他提示

您必须添加

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top