<%@page contentType="text/html"%>
<%@page pageEncoding="GB2312"%>
<%@page import="org.me.database.ConnectDataBase"%>
<%@page import="org.me.database.DatabaseTest"%><%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"><html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
        <title>beanTest</title>
    </head>
    <body>    <h1>A   bean   example</h1>
    <%
        DatabaseTest mybean=new ConnectDataBase().getDatabaseTest();
    %>
     
      <jsp:useBean id="mybean" scope="session" class="org.me.database.DatabaseTest"/>
     
        <table border="1">            
            <thead>
                <tr>
                    <th>书的ID号</th>
                    <th>标题</th>
                    <th>名字</th>
                    <th>价格</th>
                    <th>出版日期</th>
                    <th>简介</th>
                    <th>售出数量</th>
                </tr>
            </thead>
            
            <tbody>
                <tr>            <!--但是如果我不用Bean的话,用这种方式没有任何的问题。-->
                <%--
                <td><%=mybean.getBookId()%></td>
               --%>
                    <td><jsp:getProperty name="mybean" property="bookId" /></td>
                    <td><jsp:getProperty name="mybean" property="title" /></td>
                    <td><jsp:getProperty name="mybean" property="name" /></td>
                    <td><jsp:getProperty name="mybean" property="price" /></td>
                    <td><jsp:getProperty name="mybean" property="year" /></td>
                    <td><jsp:getProperty name="mybean" property="description" /></td>
                    <td><jsp:getProperty name="mybean" property="saleAmount" /></td>
                
                </tr>
            </tbody>
        </table> 
    </body>
</html>
结果显示:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
org.apache.jasper.JasperException:  The value for the useBean class attribute org.me.database.DatabaseTest is invalid.
C:/MyWeb/MyProject/build/web/beanTest.jsp(30,6)
C:\MyWeb\MyProject\nbproject\build-impl.xml:342: Java returned: 1
生成失败(总时间:5 秒)
弄了很长时间了,不明白为什么会出问题。还请高手指点,谢谢!

解决方案 »

  1.   

    不好意思,我这个问题应该当天贴出代码的,结果因为网线的问题,好长时间没有上网了。今天把代码补贴上,谢谢大家,帮帮我看看。package org.me.database;
    /**
     *
     * @author Administrator
     */
    public class DatabaseTest {
        
        private String bookId=null;
        private String title=null;
        private String name=null;
        private float price=0.0f;
        private int year=0;
        private String description=null;
        private int saleAmount=0;
        /** Creates a new instance of DatabaseTest */
        public DatabaseTest(String bookId,String title,String name,float price,
                               int year,String description,int saleAmount)
        {
            this.bookId=bookId;
            this.title=title;
            this.name=name;
            this.price=price;
            this.year=year;
            this.description=description;
            this.saleAmount=saleAmount;
        }    public String getBookId() {
            return bookId;
        }    public void setBookId(String bookId) {
            this.bookId = bookId;
        }    public String getTitle() {
            return title;
        }    public void setTitle(String title) {
            this.title = title;
        }    public String getName() {
            return name;
        }    public void setName(String name) {
            this.name = name;
        }    public float getPrice() {
            return price;
        }    public void setPrice(float price) {
            this.price = price;
        }    public int getYear() {
            return year;
        }    public void setYear(int year) {
            this.year = year;
        }    public String getDescription() {
            return description;
        }    public void setDescription(String description) {
            this.description = description;
        }    public int getSaleAmount() {
            return saleAmount;
        }    public void setSaleAmount(int saleAmount) {
            this.saleAmount = saleAmount;
        }
        
    }/*
     * ConnectDataBase.java
     *
     * Created on 2006年9月16日, 下午12:59
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */package org.me.database;import java.io.*;
    import java.sql.*;/**
     *&useUnicode=true&characterEncoding=8859_1"
     * @author Administrator
     */
    public class ConnectDataBase {
        private DatabaseTest dbTest=null;
         Connection con=null;
         Statement  statement=null;
         ResultSet rs=null;
        /** Creates a new instance of ConnectDataBase */
        public ConnectDataBase(){
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql://localhost/BookDB?user=root&password=wcj1981&useUnicode=true&characterEncoding=8859_1");
                statement=con.createStatement();
                rs=statement.executeQuery("select * from books where id='201'");
                rs.next();
               
                   dbTest=new DatabaseTest(rs.getString(1),
                                             rs.getString(2),
                                             rs.getString(3),
                                             rs.getFloat(4),
                                             rs.getInt(5),
                                             rs.getString(6),
                                             rs.getInt(7));
               }        catch(ClassNotFoundException sqle)
            {
                 sqle.printStackTrace();
            }
            catch(SQLException sqle)
            {
                 sqle.printStackTrace();
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
            finally
            {
                  try
                  {
                      rs.close();
                      statement.close();
                      con.close();
                  }
                  catch(Exception e)
                  {
                       e.printStackTrace();
                  }        }    }
        public DatabaseTest getDatabaseTest()
        {
                 return dbTest;
         }
        
        
        //这些代码是测试用的,结果显示可以正常从数据库中取出数据来。
        /*
        public static void main(String args[])
        {
                ConnectDataBase con=new ConnectDataBase();
                DatabaseTest test=con.getDatabaseTest();
                System.out.println(test.getBookId());
                System.out.println(test.getName());
                System.out.println(test.getTitle());
                System.out.println(test.getPrice());
                System.out.println(test.getYear());
                System.out.println(test.getDescription());
                System.out.println(test.getSaleAmount());   
        }
         */
       }
      

  2.   

    你的USEBEAN 标签里,是不是应该这样写
    type ="org.me.database.DatabaseTest"
    我没用过这个标签,但报错好像是这里.
    再者BEAN 应该 implements Serializable
      

  3.   

    谢谢,但是还是不行!<%
            DatabaseTest mybean=new ConnectDataBase().getDatabaseTest();
     %>
         
          <jsp:useBean id="mybean" scope="session" class="org.me.database.DatabaseTest"/>上面这一段有没有问题?看报错好象是上面的问题,是不是Bean对象只能在useBean标签中生成?
      

  4.   

    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    library-inclusion-in-archive:
    library-inclusion-in-manifest:
    compile:
    compile-jsps:
    这些是哪里来的,jar包么?看看有没有导入
    C:/MyWeb/MyProject/build/web/beanTest.jsp(30,6)
    C:\MyWeb\MyProject\nbproject\build-impl.xml:342: Java returned: 1
    这个是不是要转意,具体的要看你的工程配置。这样很难看出来
      

  5.   

    这个应该不是问题,我用的是netBeans,她每次运行的时候都会出现这些东西,应该是初始化参数。
      

  6.   

    to zx2002027(希望之星):怎么样才能把mybean添加到session中去呢? <jsp:useBean id="mybean" scope="session" class="org.me.database.DatabaseTest"/>
    这一句不是把mybean添加到session中去的吗?
      

  7.   

    List bean = new ArrayList();
    bean=con.getDatabaseTest();//你从数据库里查出来的集合;request.getSession().setAttribute("BEAN",bean);//放到SESSION中在页面用标签时,id="mybean" name="BEAN" type = "org.me.database.DatabaseTest"id是取个逻辑名,在你相应的标签中对应好.
      

  8.   

    to angel_china:
        你说的恐怕不妥,要是那样,就不用使用javabean了,直接传递个对象参数就好了
    使用javabean时,把编译成的类文件放到特定的路径下,使用jsp标签的type属性就可以
    找到这个类,系统会自动加载,不用手动提交这个bean给session,当然具体怎么个加载
    过程,我也不清楚~~to wcj1981:
        你的代码比较多,我对javabean也是新手,可以看看你编译好的class文件是不是放到了指定
    的位置,使用weblogic时应该放到web-inf/classes文件夹下。
        呵呵,要还是不行,我也调试一下你的代码,学习一下:)
      

  9.   

    javabean 是为了存取数据,是把从数据库中查出来的集合BEAN放到SESSION中.不明白我无语了.没出来就不是这里错了.
      

  10.   

    大致查出来是什么毛病了,原因出在这一句:
      //这个申明了一个mybean对象   <%
            DatabaseTest mybean=new ConnectDataBase().getDatabaseTest();
        %>
         //但是这个地方并没有用到上面生成的对象,而是又生成一个新的bean,这一点跟书上讲的不一样,不知道是编译器的问题还是什么。
          <jsp:useBean id="mybean" scope="session" class="org.me.database.DatabaseTest"/>我觉得angel_china(安琪)说的有道理,刚刚生成的bean应该加到session中,不然又生成一个新的bean,怪不得总是提示The value is invalid。可能是对session的理解不是很透彻,不知道我理解的对不?今天晚上结贴!
      

  11.   

    顺便问一句:
    ResultSet rs=stat.executeQuery("select title from books where title='Java编程指南'");这一句在数据库查询中的语法对不对,我用的是mysql数据库。但是只要我的查询中包含有汉字字符就查询不出来,请大家分析一下这个是什么原因?
    ResultSet rs=stat.executeQuery(new String(("select name from books where title='%编程%'").getBytes("ISO-8859-1"),"utf-8"));我用这个转码也不行!谢谢!