javax.servlet.ServletException:在作用域内找不到bean [name] [英] javax.servlet.ServletException: bean [name] not found within scope

查看:600
本文介绍了javax.servlet.ServletException:在作用域内找不到bean [name]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

  javax.servlet.ServletException:在范围内找不到bean 

在页面顶部。

 < jsp:useBean id =beantype =com.example.Beanscope =request/> 

该类存在于类路径中,它今天早上起作用了,我找不到未找到的内容在范围内意味着。



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

解决方案

您需要属性而不是类型属性。



以下内容:

 < ; jsp:useBean id =beantype =com.example.Beanscope =request/> 

基本以下幕后操作:

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

if(bean == null){
抛出新的ServletException(bean未在范围内找到);
}

//使用bean ...

虽然以下内容:

 < jsp:useBean id =beanclass =com.example .Beanscope =request/> 

在幕后基本上完成以下工作:



< pre class =lang-java prettyprint-override> Bean bean =(Bean)pageContext.getAttribute(bean,PageContext.REQUEST_SCOPE);

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

//使用bean ...

如果它以前工作过,而且突然没有工作,那么这意味着负责将bean放入范围的某事已停止工作。例如,一个servlet在 doGet()中执行以下操作:

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

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


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?

解决方案

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.

这篇关于javax.servlet.ServletException:在作用域内找不到bean [name]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆