Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 用Jbuilder 7开发EJB例程-开发Session Bean源码
【标  题】:用Jbuilder 7开发EJB例程-开发Session Bean源码
【关键字】:io,ld,Session,Jbuilder,EJB,源码,build,Bean,on,Be,Jbuilder,EJB,Session,Bean
【来  源】:网络

用Jbuilder 7开发EJB例程-开发Session Bean源码

Your Ad Here 用Jbuilder 7开发EJB例程-开发Session Bean源码

Jbuilder是一个很好的开发工具,你可以通过它来快速创建满足要求的EJB及其部署描述文件,在《通过实例学JBuilder 7》中,我们已经通过一个购买冰淇淋的实用程序向您介绍了如何用JBuilder 7构造一个完整的应用。今天,我们将以“购物车”程序为例,向您展示如何用Jbuilder 7快速开发EJB。

创建新的工程

1.打开Jbuilder 7,选择File|New Project,工程向导出现。

2.设置工程名为“Trader”,选择工程要保存的目录,其它选项保持不变,点击“Finish”。

Jbuilder是一个很好的开发工具,你可以通过它来快速创建满足要求的EJB及其部署描述文件,在《通过实例学JBuilder 7》中,我们已经通过一个购买冰淇淋的实用程序向您介绍了如何用JBuilder 7构造一个完整的应用。今天,我们将以“购物车”程序为例,向您展示如何用Jbuilder 7快速开发EJB。

创建新的工程

1.打开Jbuilder 7,选择File|New Project,工程向导出现。

2.设置工程名为“Trader”,选择工程要保存的目录,其它选项保持不变,点击“Finish”。

设置应用服务器

1.为了部署测试EJB,需要配置相应的应用服务器,本例采用Bea Weblogic 6.1。选择Tools|Configure Servers,出现一对话框。

2.点击左边列表中的“Weblogic Application Server 6.x+”。

3.选中“Server Setting”中的“Enable Server”,设置“General”中的“Home directory”,选择应用服务器的主目录。

3.点击“Custom”,设置“JDK installation directory”和“BEA home directory”以及“Password”。

4.点击"OK",然后重新启动Jbuilder 7。

5.点击Project|Default Project Properties。

6.选择"Server",选择Radio button??"Single server for all services in project",再选择下方的List box??"Weblogic Application Server 6.x+"。

7.点击“OK”完成设置。

创建EJB模块

1.每一个EJB必须属于一个EJB模块,选择File|New,点击"Enterprise"标签,双击"EJB Module"。

2.在Name中输入Trader,在Format中选择XML,在Version中选择EJB 2.0 compliant,点击"OK","EJB Designer"出现。

创建Session Bean

1.右击"EJB Designer"面板,选择Create EJB|Session Bean。

2.设置Session Bean的属性:Bean name中输入Trader,Interfaces中选择remote,在Session type中选择Stateful,Transaction type中选择Container。

3.增加变量"_cardHolderName"。在"EJB Designer"中右击"Trader"弹出快捷菜单,选择Add|Field。

4.进行设置:在Type中输入java.lang.String。

5.用同样的方法增加变量"_creditCardNumber"。

6.增加变量"_expirationDate",类型为"java.util.Date"。

7.用同样方法增加变量"_items",变量类型为"java.util.ArrayList"。

8.右击"EJB Designer"面板中的"Trader",选择"View Bean Source",可以看到源代码。

9.增加业务逻辑方法"addItem",向购物车中增加商品。右击"EJB Designer"面板中的"Trader",选择Add|Method。

10.输入参数的类型为"Item",它是一个类,稍后我们将创建它。

11.用同样的方法增加业务逻辑方法"removeItem",从购物车中移走某种商品。

12.用同样的方法增加业务逻辑方法"getContents",显示购物车中所有商品内容。

13.用同样的方法增加业务逻辑方法"getTotalPrice",显示购物车中所有商品的价值。

14用同样的方法增加业务逻辑方法"purchase",进行购买。

15、右击"EJB Designer"面板中的"Trader",选择"View Bean Source",可以看到此时的源代码,修改TraderBean.java源代码如下:

package trader;

import javax.ejb.*;
import java.util.Date;
import java.util.ArrayList;

public class TraderBean implements SessionBean
{
SessionContext sessionContext;
java.lang.String _cardHolderName;
java.lang.String _creditCardNumber;
java.util.Date _expirationDate;
java.util.ArrayList _items;
public void ejbCreate(String cardHolderName, String creditCardNumber, Date expirationDate) throws CreateException {
/**@todo Complete this method*/
_items=new ArrayList();
_cardHolderName=cardHolderName;
_creditCardNumber=creditCardNumber;
_expirationDate=expirationDate;
}
public void ejbRemove()
{
/**@todo Complete this method*/
}
public void ejbActivate()
{
/**@todo Complete this method*/
}
public void ejbPassivate()
{
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext)
{
this.sessionContext = sessionContext;
}
public void addItem(Item item)
{
/**@todo Complete this method*/
System.out.println("\taddItem("+item.getTitle()+"):"+this);
_items.add(item);
}
public void removeItem(Item item)
{
/**@todo Complete this method*/
System.out.println("\tremoveItem("+item.getTitle()+"):"+this);
if(!_items.remove(item)){
throw new EJBException("The item "+item.getTitle()+" is not in your cart.");
}
}
public java.util.ArrayList getContents()
{
/**@todo Complete this method*/
System.out.println("\tgetContents():"+this);
return _items;
}
public float getTotalPrice()
{
/**@todo Complete this method*/
System.out.println("\tgetTotalPrice():"+this);
float totalPrice=0f;
for(int i=0,n=_items.size();i<n;i++)
{
Item current=(Item)_items.get(i);
totalPrice+=current.getPrice();
}
return ((long)(totalPrice*100))/100f;
}
public void purchase()
{
/**@todo Complete this method*/
System.out.println("\tpurchase():"+this);
Date today=new Date();
if(_expirationDate.before(today))
{
throw new EJBException("Expiration date:"+_expirationDate);
}
System.out.println("\tPurchasing not implement yet!");
}
}

修改TraderHome.java源代码如下:
package trader;

import javax.ejb.*;
import java.util.*;
import java.rmi.*;

public interface TraderHome extends javax.ejb.EJBHome
{
public Trader create(String cardHolderName, String creditCardNumber, Date expirationDate) throws CreateException, RemoteException;
}

修改Trader.java源代码如下:
package trader;

import javax.ejb.*;
import java.util.*;
import java.rmi.*;

public interface Trader extends javax.ejb.EJBObject
{
public void addItem(Item item) throws RemoteException;
public void removeItem(Item item) throws RemoteException;
public ArrayList getContents() throws RemoteException;
public float getTotalPrice() throws RemoteException;
public void purchase() throws RemoteException;
}

新增类Item,Item.java源代码如下:

package trader;

import java.io.Serializable;

public class Item implements Serializable
{
private static final long serialVersionUID=-567896319031239L;
private String _title;
private float _price;
private String _type;

public Item(String title,float price,String type)
{
_title=title;
_price=price;
_type=type;
}

public String getTitle()
{
return _title;
}

public float getPrice()
{
return _price;
}

public String getType()
{
return _type;
}

public final boolean equals(Object o)
{
//two items are equal if they have the same class and title
if(!(o instanceof Item))
{
return false;
}
Item i=(Item)o;
return (getClass()==i.getClass())&&(_title==null?i.toString()==null:_title.equals(i._title));
}
}

至此,就可以编译并发布了。
J2ee之EJB种类介绍:【上一篇】
HTTP会话对象 VS 有状态EJB:【下一篇】
【相关文章】
  • J2ee之EJB种类介绍
  • J2ME入门-(5)CLDC API
  • Eclipse开发J2ME程序之Hello World(1)
  • Eclipse开发J2ME程序之Hello World(2)
  • Eclipse开发J2ME程序之Hello World(3)
  • j2me环境下开发HELLOWORLD程序
  • EJB设计模式(1)
  • Thinking in Java (the 2nd edition) Study Note
  • JBuilder在中文环境中光标问题的解决方案
  • EJB设计模式(4)
  • 【随机文章】
  • 发送带附件的邮件
  • 单元测试究竟是测试什么?
  • How It Works -- Master Boot Record (I think it ver
  • 基础_Linux磁盘命令
  • HTML语言剖析8:表单标记
  • 使用CTabCtrl控件实现属性页功能
  • 偶题
  • DHTML技巧--中止网页的提交
  • pb函数库之注册(Registry)函数
  • 动网论坛有史以来最大的安全漏洞(2)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.