亮子的博客

为伊消得人憔悴
posts - 5, comments - 2, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Jsp中访问JavaBean入门

Posted on 2006-07-20 15:19 亮子 阅读(1491) 评论(0)  编辑  收藏

先来介绍一下JavaBean
什么是JavaBean?
  JavaBean是一种可重复使用,且跨平台的软件组织.
  分两种:有用户界面的JavaBean
          没有用户界面的JavaBean,主要用来处理事务的JavaBean.通常所指的就是此种JavaBean.
JavaBean的特性?
  JavaBean是一个Public类
  JavaBean有一个不带参数的构造方法 
 JavaBean通过setter,getter来设置和访问属性.
JSP访问JavaBean?
      有两种办法来访问JavaBean:
        1)通过程序代码
        2)通过JSP标签来访问JavaBean
如何通过JSP标签来访问JavaBeans?
        1)导入JavaBean类: <%@ page import="my.SampleBean"%>
        2)声明JavaBean对象 : 使用<jsp:userBean id="myBean" class="my.SampleBean" scope="Session"/>
                id代表对象的变量名
                class指定JavaBean类名,必须是JavaBean的完整名称.
                scope指定JavaBean对象的范围
        3)访问JavaBean属性:
                访问JavaBean属性: <jsp:getProperty name="myBean" property="xxx"/>
                设置JavaBean属性: <jsp:setProperty name="myBean" property="xxx" value="0"/>
JavaBean的范围.
        scope属性决定JavaBean对象存在的范围.scope可选值有四个:1)page 2)request 3)session 4)application. 默认page.

举个例子先,

jsp文件如下:

 1 <% @page contentType = " text/html "   %>
 2 <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > 
 3<%@page import="lonny.dbtest.User"%>  <!-- 导入JavaBean类-->
 4<html>
 5
 6<head>
 7        <title>JSP Page</title>
 8</head>
 9<body bgcolor="#FFFFFF">
10<jsp:useBean id="myBean" scope="page" class="lonny.dbtest.User"  /> <!--声明JavaBean对象 -->
11        my name is <jsp:getProperty name="myBean" property="userName" />  <!--访问JavaBean属性 -->
12        <%
13                User user = null;
14                String scope = null;
15                user = (User)request.getAttribute("myBean");
16                if (user != null) scope  = "request";
17                
18                user = (User)session.getAttribute("myBean");
19                if (user != null) scope  = "session";
20                
21                user = (User)application.getAttribute("myBean");
22                if (user != null) scope  = "application";
23                
24                if (user == null) scope = "page";
25        %>        
26        scope type  is <%=scope%>
27</body>
28</html>
29
30

javabean User类文件User.java内容为:

 1 package  lonny.dbtest;
 2 public   class  User  {
 3          public  User(String userID, String userName, Sex sex, String birthday, String password) {
 4                  this .userID  =  userID;
 5                  this .userName  =  userName;
 6                  this .sex  =  sex.getSex();
 7                  this .birthday  =  birthday;
 8                  this .password  =  password;
 9         }

10          public  String getBirthday()  {
11                  return  birthday;
12         }

13          public   void  setBirthday(String birthday)  {
14                  this .birthday  =  birthday;
15         }

16          public   boolean  getSex()  {
17                  return  sex;
18         }

19          public   void  setSex(Sex sex)  {
20                  this .sex  =  sex.getSex();
21         }

22          public  String getUserID()  {
23                  return  userID;
24         }

25          public   void  setUserID(String userID)  {
26                  this .userID  =  userID;
27         }

28          public  String getUserName()  {
29                  return  userName;
30         }

31          public   void  setUserName(String userName)  {
32                  this .userName  =  userName;
33         }

34          public  String getPassword()  {
35                  return  password;
36         }

37          public   void  setPassword(String password)  {
38                  this .password  =  password;
39         }

40         
41          private  String userID;
42          private  String userName;
43          private   boolean  sex;
44          private  String birthday;
45          private  String password;
46 }

47

注意:上面的程序经编译、部署,访问会出现java.lang.InstantiationException异常.
这是因为JavaBean User类需要有一个不带参数的构造器.
添加此构造器后问题解决:

        public  User() {
        
       }


 


只有注册用户登录后才能发表评论。


网站导航: