javax.servlet.ServletException: bean [name] 在范围内未找到 [英] javax.servlet.ServletException: bean [name] not found within scope
问题描述
我收到此错误:
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?
推荐答案
您需要 class
属性而不是 type
属性.
You need the class
attribute instead of the type
attribute.
以下内容:
<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" />
在幕后基本上做了以下工作:
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 ...
如果它之前工作过并且突然"没有工作,那么这意味着负责将 bean 放入作用域的东西已经停止工作.例如,在 doGet()
中执行以下操作的 servlet:
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);
也许你直接通过 URL 调用了 JSP 页面,而不是通过 URL 调用 Servlet.如果您想禁用对 JSP 页面的直接访问,请将它们放在 /WEB-INF
中并转发给它.
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.
这篇关于javax.servlet.ServletException: bean [name] 在范围内未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!