日期:2014-05-20  浏览次数:20666 次

The value for the useBean class attribute DisplayFormBean is invalid
<%@   page   contentType= "text/html;charset=gb2312 "   import= "java.util.Vector "   %>
<html>
    <head>
    <jsp:useBean   id= "displayformID "   class= "DisplayFormBean "/>
     
       
        <title> My   JSP   '显示数据库e </title>
       
<meta   http-equiv= "pragma "   content= "no-cache ">
<meta   http-equiv= "cache-control "   content= "no-cache ">
<meta   http-equiv= "expires "   content= "0 ">        
<meta   http-equiv= "keywords "   content= "keyword1,keyword2,keyword3 ">
<meta   http-equiv= "description "   content= "This   is   my   page ">
<!--
<link   rel= "stylesheet "   type= "text/css "   href= "styles.css ">
-->

    </head>
   
    <body>
      <h1> JSP显示数据库 </h1>
      <%   out.print(displayformID.connect());   %>
      <%   out.print(displayformID.select());   %>
      <p> 显示数据库表中的   列表
      <%   Vector   aResult=displayformID.getResult();   %>
      <table>
      <%   for(int   i=0;i <aResult.size();i++)   {%>
      <tr>
      <td>
      <%   out.print(aResult.elementAt(i));   %>
      </td>
      </tr>
      <%   }   %>
    </body>
</html>

------解决方案--------------------
(转载)详解 Tomcat: The value for the useBean class attribute is invalid 问题

使用Tomcat 常见 "The value for the useBean class attribute is invalid " 错误。该错误是指 JSP 中给定的 useBean 标签的 class 属性的值无效(不是 Bean 的属性值)。

在说明这个问题前,先看看有关的 Tomcat 源代码(org.apache.jasper.compiler.Generator):


if (beanName == null) {
try {
Class bean = ctxt.getClassLoader().loadClass(klass);
int modifiers = bean.getModifiers();
if (!Modifier.isPublic(modifiers) ||
Modifier.isInterface(modifiers) ||
Modifier.isAbstract (modifiers)) {
throw new Exception( "Invalid bean class modifier ");
}
// Check that there is a 0 arg constructor
bean.getConstructor(new Class[] {});
generateNew = true;
} catch (Exception e) {
// Cannot instantiate the specified class
if (ctxt.getOptions().getErrorOnUseBeanInvalidClassAttribute()) {
err.jspError(n, "jsp.error.invalid.bean ", klass);
}
}
}


可见错误可能的原因包括:

1. 在编译 JSP 时(不是运行时),指定的 Bean 类没找到
2. Bean 虽然找到了,但是它不是 public 的,或者找到的 class 文件是 interface 或抽象类
3. Bean 类中没有 public 的默认构建函数

第二点很明显,不用多解释,最经常发生的情况是 Bean 类忘了声明为 public 。

第三点中需要注意的是,如果你的 Bean 类没有提供任何构造函数,将自动生成一个默认构建函数,这没有问题。但是,如果你有构造函数,则不会自动生成该默认构造函数。经常被忽略的问题是写了默认构造函数却不是 public 的。