在使用JavaBean时遇到的问题:The value for the useBean class attribute is invalid

来源:互联网 发布:数据类型 js 编辑:程序博客网 时间:2024/04/30 08:41

The value for the useBean class attribute is invalid ,

The value for the useBean class attribute java.lang.Integer is invalid.

The value for the useBean class attribute java.util.List is invalid

是因为在JSP中使用了代码:

<jsp:useBean id="ShowCharge" class="java.lang.Boolean" scope="request"/>

可能是JDK版本问题导致编译的时候找不到Boolean类。

怎么办呢?

在JSP页面:<jsp:useBean id="ShowCharge" class="java.lang.Boolean" scope="request"/>

会显示错误信息:The constructor Boolean() is undefined即没有对应的构造函数

实际上代码<jsp:useBean id="ShowCharge" class="java.lang.Boolean" scope="request"/>

等价于:

Boolean ShowCharge = (Boolean)request.getAttribute("ShowCharge");
    if(ShowCharge == null){
        Boolean ShowCharge = new Boolean();
        request.setAttribute("ShowCharge",ShowCharge);
    }

那么JDK1.6中是没有Boolean()构造函数的,所以会报错。。。。;

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

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

对于

The value for the useBean class attribute is invalid ,

解决方法,用等价代码去替换如
将:<jsp:useBean id="ShowCharge" class="java.lang.Boolean" scope="request"/>

替换成:

Boolean ShowCharge = (Boolean)request.getAttribute("ShowCharge");
    if(ShowCharge == null){
        Boolean ShowCharge = new Boolean(false );//注意这里换成带参数的构造方法
        request.setAttribute("ShowCharge",ShowCharge);
    }

对于

The value for the useBean class attribute java.lang.Integer is invalid.

解决方法,用等价代码去替换如

将:<jsp:useBean id="MaxDocDispIdx " class="java.lang.Integer" scope="request"/>

替换成:

Integer MaxDocDispIdx = (Integer)request.getAttribute("MaxDocDispIdx");
    if(MaxDocDispIdx == null){
        MaxDocDispIdx = new Integer(0);
        request.setAttribute("MaxDocDispIdx",MaxDocDispIdx);
    }

对于

The value for the useBean class attribute java.util.List is invalid

解决方法是将:

<jsp:useBean id="DocBeanList" class="java.util.List" scope="session" />

换成

<jsp:useBean id="DocBeanList" class="java.util.ArrayList" scope="session" />

 

经过测试发现另一种方法可以实现:

如:<jsp:useBean id="ShowCharge" class="java.lang.Boolean" scope="request"/>

改成<jsp:useBean id="ShowCharge" type="java.lang.Boolean" scope="request"/>

即将class 改成type.对以上Integer,List类型都可以。

 

还有对于class里的是一个object也可能是上诉情况。

 

原创粉丝点击