Jbuilder是一個很好的開發工具,你可以通過它來快速創建滿足要求的EJB及其部署描述文件,在《通過實例學JBuilder 7》中,我們已經通過一個購買冰淇淋的實用程序向您介紹了如何用JBuilder 7構造一個完整的應用。本文以“購物車”程序為例,向您展示如何用Jbuilder 7快速開發EJB。
創建新的工程
1.打開Jbuilder 7,選擇FileNew PRoject,工程向導出現。
2.設置工程名為“Trader”,選擇工程要保存的目錄,其它選項保持不變,點擊“Finish”。
設置應用服務器
1.為了部署測試EJB,需要配置相應的應用服務器,本例采用Bea Weblogic 6.1。選擇ToolsConfigure Servers,出現一對話框。
2.點擊左邊列表中的“Weblogic application Server 6.x+”。
3.選中“Server Setting”中的“Enable Server”,設置“General”中的“Home Directory”,選擇應用服務器的主目錄。
4.點擊“Custom”,設置“JDK installation directory”和“BEA home directory”以及“PassWord”。
5.點擊"OK",然后重新啟動Jbuilder 7。
6.點擊ProjectDefault Project Properties。
7.選擇"Server",選擇Radio button??"Single server for all services in project",再選擇下方的List box??"Weblogic Application Server 6.x+"。
8.點擊“OK”完成設置。
創建EJB模塊
1.每一個EJB必須屬于一個EJB模塊,選擇FileNew,點擊"Enterprise"標簽,雙擊"EJB Module"。
2.在Name中輸入Trader,在Format中選擇xml,在Version中選擇EJB 2.0 compliant,點擊"OK","EJB Designer"出現。
創建session Bean
1.右擊"EJB Designer"面板,選擇Create EJBSession Bean。
2.設置Session Bean的屬性:Bean name中輸入Trader,Interfaces中選擇remote,在Session type中選擇Stateful,Transaction type中選擇Container。
3.增加變量"_cardHolderName"。在"EJB Designer"中右擊"Trader"彈出快捷菜單,選擇AddField。
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",選擇AddMethod。
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
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));
} }
新聞熱點
疑難解答