小丐的blog

          Java

          EJB調用EJB

          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 >


          ?

          posted on 2006-09-13 23:19 littlegai 閱讀(875) 評論(0)  編輯  收藏 所屬分類: Weblogic


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          My Technique Blog

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 罗山县| 佳木斯市| 武山县| 松阳县| 陈巴尔虎旗| 德庆县| 保康县| 鞍山市| 邵阳县| 巨鹿县| 秭归县| 德保县| 石狮市| 友谊县| 安达市| 辽阳县| 岱山县| 新乐市| 泸西县| 定远县| 梨树县| 武义县| 凤山市| 水城县| 铅山县| 白河县| 韶关市| 梅河口市| 康保县| 竹溪县| 盖州市| 临湘市| 庆阳市| 安远县| 邛崃市| 凤翔县| 福海县| 依安县| 乌拉特后旗| 建瓯市| 天长市|