1.新建一個CMP的Entity Bean。新的Entity Bean被映射到Sql Server 2000的Northwind Databse的Region表。在按照創建向導完成該Entity Bean的建立以后,可以得到如下所示的代碼:
package
?RegionEJB;

import
?java.util.Collection;
import
?javax.ejb.CreateException;

import
?weblogic.ejb.GenericEntityBean;


/**?*/
/**
?*?@ejbgen:entity
?*???ejb-name?=?"RegionBean"
?*???data-source-name?=?"NorthwindDataSource"
?*???table-name?=?"Region"
?*???prim-key-class?=?"java.lang.Integer"
?*
?*?@ejbgen:jndi-name?remote="ejb.RegionBeanRemoteHome"
?*???local?=?"ejb.RegionBeanHome"
?*
?*?@ejbgen:file-generation?local-class?=?"true"?local-class-name?=?"Region"?local-home?=?"true"?local-home-name?=?"RegionHome"?remote-class="true"?remote-class-name?=?"RegionRemote"?remote-home="true"?remote-home-name?=?"RegionRemoteHome"?value-class?=?"false"?value-class-name?=?"RegionValue"?pk-class?=?"true"
?*?@ejbgen:finder?ejb-ql="SELECT?OBJECT(o)?from?RegionBean?as?o"?generate-on="Local"?signature="Collection?findAll()"
?
*/
public
?
abstract
?
class
?RegionBean?
??
extends
?GenericEntityBean

{

??
public
?java.lang.Integer?ejbCreate(Integer?RegionID)
????
throws
?CreateException

??
{
????setRegionID(RegionID);

????
return
?
null
;
??}
??
??
public
?
void
?ejbPostCreate(Integer?RegionID)?
????
throws
?CreateException

??
{}
??
/**?*/
/**
???*?@ejbgen:cmp-field?column?=?"RegionID"
???*??primkey-field="true"
???*?@ejbgen:local-method
???
*/
??
public
?
abstract
?Integer?getRegionID();

??
/**?*/
/**
???*?@ejbgen:local-method
???
*/
??
public
?
abstract
?
void
?setRegionID(Integer?val);
??

??
/**?*/
/**
???*?@ejbgen:cmp-field?column?=?"RegionDescription"
???*?@ejbgen:local-method
???
*/
??
public
?
abstract
?String?getRegionDescription();

??
/**?*/
/**
???*?@ejbgen:local-method
???
*/
??
public
?
abstract
?
void
?setRegionDescription(String?val);
??
}
上面的代碼中已經手動添加了一個叫做findAll的方法,該方法可以得到RegionBean的集合。
2.現在建立一個Session Bean。通過這個Session Bean調用RegionBean的findAll方法,并將之保存到一個數組中。代碼如下:
package
?RegionEJB;
import
?java.io.Serializable;
import
?java.rmi.RemoteException;
import
?java.util.Collection;
import
?java.util.Iterator;
import
?javax.ejb.
*
;
import
?weblogic.ejb.
*
;
import
?javax.naming.Context;
import
?javax.naming.InitialContext;
import
?javax.ejb.SessionContext;
import
?javax.ejb.SessionBean;
import
?javax.ejb.CreateException;
/**
?*?@ejbgen:session?type="Stateless"
?*???ejb-name?=?"RegionView"
?*
?*?@ejbgen:jndi-name?local="ejb.RegionViewLocalHome"
?*???remote?=?"ejb.RegionViewRemoteHome"
?*
?*?@ejbgen:file-generation?remote-class?=?"true"?remote-class-name?=?"RegionViewRemote"?remote-home?=?"true"?remote-home-name?=?"RegionViewHome"?local-class="true"?local-class-name?=?"RegionViewLocal"?local-home="true"?local-home-name?=?"RegionViewLocalHome"
?*?@ejbgen:ejb-local-ref?type="Entity"?name="ejb/Region"?link="RegionBean"?jndi-name="ejb.RegionBeanHome"?local="Region"?home="RegionHome"?
?
*/
public
?
class
?RegionView
??
extends
?GenericSessionBean
??
implements
?SessionBean
{
????SessionContext?sessionContext;
????Context?context;
????RegionHome?regionHome;
??
public
?
void
?ejbCreate()?
throws
?CreateException?{
????
//
?Your?code?here
??}
??
public
?
void
?setSessionContext(SessionContext?sessionContext)
??{
????
this
.sessionContext
=
sessionContext;
????
try
????{
????????initRegionHome();
????}
????
catch
(Exception?ex)
????{
????????
throw
?
new
?EJBException(ex.getMessage());
????}
??}
??
private
?
void
?initRegionHome()?
throws
?Exception
??{
????
final
?String?ENTITY_NAME
=
"
java:comp/env/ejb/Region
"
;
????
this
.context
=
new
?InitialContext();
????
if
(
null
==
regionHome)
????{
????????
try
????????{
????????????Object?obj
=
context.lookup(ENTITY_NAME);
????????????regionHome
=
(RegionHome)obj;
????????}
????????
catch
(Exception?ex)
????????{
????????????
throw
?
new
?EJBException(ex.getMessage());
????????}
????}
??}
??
/**
???*?@ejbgen:local-method
???
*/
??
public
?
void
?addRegion(
int
?id,String?desc)?
throws
?Exception
??{
????
try
????{
????????Region?region
=
regionHome.create(
new
?Integer(id));
????????region.setRegionDescription(desc);
????}
????
catch
(Exception?ex)
????{
????????
throw
?
new
?Exception(ex.getMessage());
????????
????}
????
??}
??
/**
???*?@ejbgen:local-method
???
*/
??
public
?Object[]?getAllRegionView()?
throws
?Exception
??{
????
try
????????{
???????????Collection?collection
=
regionHome.findAll();
???????????
int
?size
=
collection.size();
???????????
if
(size
==
0
)
????????????
throw
?
new
?Exception(
"
Null?Ejb?Object?Found!!
"
);
???????????RegionInfo[]?regionInfo
=
new
?RegionInfo[collection.size()];
???????????Iterator?iter
=
collection.iterator();
???????????
int
?regionIndex
=
0
;
???????????
while
(iter.hasNext())
???????????{
???????????????RegionEJB.Region?ejb
=
(RegionEJB.Region)iter.next();
???????????????regionInfo[regionIndex]
=
new
?RegionInfo(ejb.getRegionID(),ejb.getRegionDescription());
???????????????regionIndex
=
regionIndex
+
1
;
???????????????
//
regionInfo[regionIndex]=new?RegionInfo(new?Integer(regionIndex),iter.next().getClass().toString());
???????????}
???????????
return
?regionInfo;
????????}
????????
catch
(Exception?e)
????????{
???????????
throw
?
new
?Exception(e.getMessage());
????????}
????
??}
???
public
?
static
?
class
?RegionInfo?
implements
?Serializable
????{
????????
public
?java.lang.Integer?regionID;
????????
public
?String?regionDescription;
????????
????????
public
?RegionInfo()
????????{
????????}
????????
public
?RegionInfo(java.lang.Integer?regionID,String?regionDescription)
????????{
????????????
this
.regionID
=
regionID;
????????????
this
.regionDescription
=
regionDescription;
????????}
????}
}
在注釋中用@ejbgen來配置Weblogic Workshop所提供的EJBGenerate工具生成EJB。在上面的代碼中,session type是手動添加的。另外為了調用其它的EJB,還要配置ejb-local-ref。因為被發布在同一個服務器中,所以使用本地調用即可。這里提取這段代碼來看一下:
*
?@ejbgen:ejb
-
local
-
ref?type
=
"
Entity
"
?name
=
"
ejb/Region
"
?link
=
"
RegionBean
"
?jndi
-
name
=
"
ejb.RegionBeanHome
"
?local
=
"
Region
"
?home
=
"
RegionHome
"
?
因為被調用的對象類型為Entity Bean,所以在type屬性中選擇Entity。如果被調用對象為Session Bean,則可選擇Session。name為對該引用定義指定的名字。link屬性填入被引用EJB組件的名稱。jndi-name填入被引用EJB的JNDI,local填入本地接口的名稱,home填入home接口的名稱。填寫的時候即可以手動輸入,也可以在右側的property editor中填寫。
這里調用findAll的方法getAllRegionView前面的注釋中需要添加ejbgen:local-method以定義為本地方法,這樣ejbgen才會在生成Bean代碼的時候找到它。
3.在jsp頁面中調用這個Session Bean的方法。
首先添加一個EJB控件來包容該EJB調用。然后再在頁面流中實例化一個EJB控件對象。然后為頁面流添加一個方法getAllRegion,最后配置一下JSP頁面中的netui標簽以展現數據。下面是getAllRegion方法:
public
?Object[]?getAllRegion()
????{
????????
try
????????{
????????????
return
?regionViewCtrl.getAllRegionView();
????????}
????????
catch
(Exception?ex)
????????{
????????????
return
?
null
;
????????}
????}
下面是JSP頁面的內容:
<
%@?page?
language
="java"
?contentType
="text/html;charset=UTF-8"
%
>
<
%@?taglib?
uri
="netui-tags-databinding.tld"
?prefix
="netui-data"
%
>
<
%@?taglib?
uri
="netui-tags-html.tld"
?prefix
="netui"
%
>
<
%@?taglib?
uri
="netui-tags-template.tld"
?prefix
="netui-template"
%
>
<
netui:html
>
????
<
head
>
????????
<
title
>
????????????Web?Application?Page
????????
</
title
>
????
</
head
>
????
<
body
>
????????
<
p
>
????????
????????Hi!You?Have?Clicked?Ok?Button!
????????
</
p
>
???????
<
netui-data:repeater?
dataSource
="{pageFlow.allRegion}"
>
????????
<
netui-data:repeaterHeader
>
????????????
<
table?
border
="1"
>
????????????????
<
tr
>
????????????????????
<
td
><
b
>
ID
</
b
></
td
>
??
????????????????????
<
td
><
b
>
Description
</
b
></
td
>
????
????????????????
</
tr
>
????????
</
netui-data:repeaterHeader
>
????????
<
netui-data:repeaterItem
>
????????????
<
tr
>
????????????????
<
td
>
????????????????????
<
netui:label?
value
="{container.item.regionID}"
?
/>
????????????????
</
td
>
????????????????
<
td
>
????????????????????
<
netui:label?
value
="{container.item.regionDescription}"
?
/>
????????????????
</
td
>
????????????
</
tr
>
????????
</
netui-data:repeaterItem
>
????????
<
netui-data:repeaterFooter
>
????????????
</
table
>
????????
</
netui-data:repeaterFooter
>
????
????
</
netui-data:repeater
>
????
</
body
>
</
netui:html
>
?

]]>