程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> J2ee應用程序設計――多層框架

J2ee應用程序設計――多層框架

編輯:J2EE
本人從事j2ee的設計已又多年,對j2ee框架,和其設計模式有較深,以下是我在開發過程中提出的一個框架,供大家參考,此框架使用以下設計模式:session façade,general Attibute method,jdbc for reading,business delegate,home factory,data Access command bean 綜述關於J2EE的框架設計的中心思想是:one transaction ,one invocation,,現今很多相應的設計思想和模式都是基於此。這個思想將貫穿於這個文章中,為了做到高性能,可復用性,可擴展性,將其框架分為五層:persistence 層,domain層,server 層,aplication 層,presentation 層,以下我將對各層做相應的闡述,並用wsad做一個實例,以供大家查考。 persistence 層作用:用於訪問底層資源,提供公共接口,例如javamail,jms等,以下以連接池為例模型:data access command bean 建立一些用於與連接池打交道的class,將domain層與數據庫隔離。相應操作:(使用wsad)打開java perspective,在ejbmodel目錄下建立一個package,命名為com.aostar.persisitence 在相應的package裡建立一個新的class,命名為DataBase.class,用於取得連接編寫代碼如下: package com.aostor.persistence; import java.sql.Connection; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class Database { public static Connection getConnection() throws Exception { Context ctx=null; Try { ctx=new InitialContext(); DataSource dt=(DataSource)ctx.lookup("jdbc/template"); return dt.getConnection(); } catch(Exception ex) { ex.printStackTrace(); throw new Exception("the class of Database error:"+ex.toString()); } finally { if(ctx!=null) { try { ctx.close(); } catch(Exception ex) { } ctx=null; } } } } 然後做一個dacb,建立新class,DataAccessBean,采用的模型:proxy model,代碼如下 package com.aostor.persistence; import java.math.BigDecimal; import java.sql.Array; import java.sql.Blob; import java.sql.Clob; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; public class DataAccessBean { /** * varIEbles * */ String sql=""; String jdbcName=Database.Oracle; Connection con=null; PreparedStatement pstt=null; /** * setSql * * @param value String */ public void setSql(String value) throws NotConnectionException { try { if(con==null) { con=Database.getConnection(jdbcName); } sql=value; pstt=con.prepareStatement(value,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); } catch(Exception ex) { ex.printStackTrace(); throw new NotConnectionException(Database.Oracle+"can't be connection"); } } /** * setSql * * @param value String * @param resultSetType int * @param resultSetCurrent int */ public void setSql(String value,int resultSetType,int resultSetCurrent) throws NotConnectionException { try { if(con==null) { con=Database.getConnection(jdbcName); } sql=value; pstt=con.prepareStatement(value,resultSetType,resultSetCurrent); } catch(Exception ex) { ex.printStackTrace(); throw new NotConnectionException(Database.Oracle+"can't be connection"); } } /** * close * */ public void close() throws NotCloseException { if(pstt!=null) { try { pstt.close(); } catch(Exception ex) { ex.printStackTrace(); throw new NotCloseException(ex.toString()); } pstt=null; } if(con!=null) { try { con.close(); } catch(Exception ex) { ex.printStackTrace(); throw new NotCloseException(ex.toString()); } con=null; } sql=""; } /** * setJdbcName * @param jdbcName String */ public void setJdbcName(String jdbcName) { if(con!=null) { try { close(); } catch(NotCloseException ex) { } } this.jdbcName=jdbcName; } /** * setString * @param value String * @param col,int */ public void setString(int col,String value) { try { pstt.setString(col,value); }catch(SQLException ex){ } } /** * setInt * @param value int * @param col int */ public void setInt(int col,int value){ try{ pstt.setInt(col,value); }catch(SQLException ex){ } } /** * setFloat * @param value Float * @param col int */ public void setFloat(int col,float value){ try{ pstt.setFloat(col,value); }catch(SQLException ex){ } } /** * setDouble * @param value Double * @param col int */ public void setDouble(int col,double value){ try{ pstt.setDouble(col,value); }catch(SQLException ex){ } } /** * setLong * @param value long * @param col int */ public void setLong(int col,long value){ try{ pstt.setLong(col,value); }catch(SQLException ex){ } } /** * setByte * @param value byte * @param col int */ public void setByte(int col,byte value){ try{ pstt.setByte(col,value); }catch(SQLException ex){ } } /** * setBytes * @param value byte[] * @param col int */ public void setBytes(int col,byte[] value){ try{ pstt.setBytes(col,value); }catch(SQLException ex){ } } /** * setShort * @param value Short * @param col int */ public void setShort(int col,short value){ try{ pstt.setShort(col,value); }catch(SQLException ex){ } } /** * setBigDecimal * @param value BigDecimal * @param col int */ public void setBigDecimal(int col,BigDecimal value){ try{ pstt.setBigDecimal(col,value); }catch(SQLException ex){ } } /** * setBoolean * @param value boolean * @param col int */ public void setBoolean(int col,boolean value){ try{ pstt.setBoolean(col,value); }catch(SQLException ex){ } } /** * setDate * @param value Date * @param col int */ public void setDate(int col,Date value){ try{ pstt.setDate(col,value); }catch(SQLException ex){ } } /** * setTime * @param value Time * @param col int */ public void setTime(int col,Time value){ try{ pstt.setTime(col,value); }catch(SQLException ex){ } } /** * setTimestamp * @param value Timestamp * @param col int */ public void setTimestamp(int col,Timestamp value){ try{ pstt.setTimestamp(col,value); }catch(SQLException ex){ } } /** * setClob * @param value Clob * @param col int */ public void setClob(int col,Clob value){ try{ pstt.setClob(col,value); }catch(SQLException ex){ } } /** * setBlob * @param value Blob * @param col int */ public void setBlob(int col,Blob value){ try{ pstt.setBlob(col,value); }catch(SQLException ex){ } } /** * setObject * @param value int * @param col int */ public void setObject(int col,Object value){ try{ pstt.setObject(col,value); }catch(SQLException ex){ } } /** * setArray * @param value Array * @param col int */ public void setArray(int col,Array value){ try{ pstt.setArray(col,value); }catch(SQLException ex){ } } /** * setNull * @param value int * @param col int */ public void setNull(int col,int value){ try{ pstt.setNull(col,value); }catch(SQLException ex){ } } /** * getMetaData * @return MetaData */ public ResultSetMetaData getMetaData() throws SQLException{ try{ return pstt.getMetaData(); }catch(SQLException ex){ throw ex; } } /** * execute */ public boolean execute() throws SQLException{ try{ doLog(); return pstt.execute(); }catch(SQLException ex){ doError(); ex.printStackTrace(); throw ex; } } /** * executeQuery */ public ResultSet executeQuery() throws SQLException{ try{ doLog(); return pstt.executeQuery(); }catch(SQLException ex){ doError(); ex.printStackTrace(); throw ex; } } /** * executeUpdate */ public int executeUpdate() throws SQLException{ try{ doLog(); return pstt.executeUpdate(); }catch(SQLException ex){ doError(); ex.printStackTrace(); throw ex; } } /** * doLog */ void doLog(){ } /** * doError() */ void doError(){ } } 兩個異常: package com.aostor.persistence; public class NotCloseException extends Exception { /** * Constructor for NotCloseException */ public NotCloseException() { super(); } /** * Constructor for NotCloseException */ public NotCloseException(String arg0) { super(arg0); } } package com.aostor.persistence; public class NotConnectionException extends Exception { /** * Constructor for NotConnectionException */ public NotConnectionException() { super(); } /** * Constructor for NotConnectionException */ public NotConnectionException(String arg0) { super(arg0); } } 在sever perspective中建立連接池打開服務器配置文件,sever-cfg.XML 選定視圖界面中Data source 以下以oralce為例先建立jdbc driver Name:OracleIdbcDriver Description:Oracle9i JDBC Driver 前兩項為名稱和描述,可隨意配置 Implementation class name oracle.jdbc.pool.OracleConnectionPoolDataSource URL prefix:jdbc:oracle:thin Class:c:/oracle/ora92/jdbc/lib/classes12.zip 然後建立Data Source Name:Session Persistence Datasource JNDI name:jdbc/template Database name:ao8226 Default user id:feinst Default user user passWord:* 其他缺省,或為空最後建立resource property name:URL (大寫) Type:java.lang.String Value:jdbc:oracle:thi:@ao-8226(服務器名稱):1521:ao8226(數據庫名) Description : 說明:此層可由專人寫出,打包,以後,由各位導入既可,但連接池必須自己配置,可方便大家做模塊測試之用. Database JDBC Driver Oracle 8i Oracle Thin Driver Version 8.1.6.0.1Merant DataDirect Oracle Driver 2.2 Oracle 9i Oracle Thin Driver Version 8.1.7 Microsoft SQL Server 7.0 Merant DataDirect SQL Server Driver 2.2JTurbo SQL Server Driver 2.0i-Net Sprinta 4.15 SQL Server Driver MySQL Max 3.23.48 mm.mysql 2.0.4 Informix 7.3.0 TC3 Informix JDBC Driver for Informix Dynamic Server 2.10.JC1N361Merant DataDirect Informix Driver 2.2 Cloudscape 4.0 Cloudscape 4.0 JDBC Driver 4.0Cloudscape 4.0 RMI JDBC Driver 4.0 MySQL 3.28 with InnoDB tables MySQL 2.0.4 driver Sybase 12.5 jConnect for JDBC 5.5 以上為其他數據的jdbc引擎,配置基本相同 domain層此層主要是用一些bmp或cmp封裝usercase的buisness thing,至於選用何種bean,視情況而定,一般來說,當數據庫的table已存在,大多用bmp, bmp 其命名規則如下: com.模塊名稱.domain.bean名稱進入j2ee perspective,建立一個新的enterprise bean 編寫代碼:考慮到ejb server和container的版本不同,如果是ejb1.x,只寫一套接口,如EJBHome和EJBObject的接口,如果是ejb2.0,需寫兩套接口,EJBHome,EJBObject,和EJBLocalHome, EJBLocalObject接口,兩者沒有多大的區別,只是前者要拋出RemoteException異常,需要指出的是接口的中的方法不需要自己寫,因為可以在其對應的bean中,用工具成生。Bean中method名稱前有ejb的為容器調用方法,代碼編寫原則是如果method中的放回值和參數,能夠用接口的,必須用接口,已方便以後的擴展如圖:(生成Home接口和遠端接口) a、 TdomainHome(Home接口) import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; import javax.ejb.FinderException; /** * Home interface for Enterprise Bean: TDomain */ public interface TDomainHome extends javax.ejb.EJBHome { /** * Creates an instance from a key for Entity Bean: TDomain */ public TDomain create() throws javax.ejb.CreateException, java.rmi.RemoteException; /** * Finds an instance using a key for Entity Bean: TDomain */ public TDomain findByPrimaryKey(TDomainKey primaryKey) throws javax.ejb.FinderException, java.rmi.RemoteException; /** * ejbCreate * @param id String * @param name String * @param birth String * @param sex char * @return TDomainKey */ public TDomain create(String id,String name,String birth,char sex) throws CreateException, java.rmi.RemoteException; } b、 Tdomain(遠端接口) import java.rmi.RemoteException; import java.util.HashMap; import javax.ejb.EJBObject; /** * Remote interface for Enterprise Bean: TDomain */ public interface TDomain extends javax.ejb.EJBObject { /** * generic Attribute method * @param Attri hashMap * */ public void changeValue(HashMap attri) throws java.rmi.RemoteException; } c、TDomainBean import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashMap; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.EntityBean; import javax.ejb.EntityContext; import javax.ejb.FinderException; import javax.ejb.RemoveException; import com.aostor.persistence.Database; /** * Bean implementation class for Enterprise Bean: TDomain */ public class TDomainBean implements javax.ejb.EntityBean { private javax.ejb.EntityContext myEntityCtx; /** * getEntityContext */ public javax.ejb.EntityContext getEntityContext() { return myEntityCtx; } /** * setEntityContext */ public void setEntityContext(javax.ejb.EntityContext ctx) { myEntityCtx = ctx; } /** * unsetEntityContext */ public void unsetEntityContext() { myEntityCtx = null; } /** l ejbActivate l 容器管理bean時,當bean進入活躍時,應做的一些操作 */ public void ejbActivate() { //id在table中對應pk id=((TDomainKey)myEntityCtx.getPrimaryKey()).getValue(); } /** * ejbCreate */ public TDomainKey ejbCreate() throws javax.ejb.CreateException { return null; } /** l ejbFindByPrimaryKey與home接口的findByPriamryKey對應 l 用主鍵查找bean */ public TDomainKey ejbFindByPrimaryKey(TDomainKey primaryKey) throws javax.ejb.FinderException { DataAccessBean dab=new DataAccessBean(); Try { //connection dab.setSql("select * from template1 where id =? "); dab.setString(1,primaryKey.getValue()); //execute:insert into template (id) values ? ResultSet rs=dab.executeQuery(); rs.next(); id=rs.getString("id"); return primaryKey; } catch(Exception ex) { throw new FinderException("the class of TDomain :"+ex.toString()); } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex) { } dab=null; } } } /** l ejbLoad l bean活躍時應裝入的數據項 */ public void ejbLoad() { DataAccessBean dab=new DataAccessBean(); Try { //connection dab.setSql("select * from template1 where id =?"); dab.setString(1,id); //execute:insert into template (id) values ? ResultSet rs=dab.executeQuery(); if(rs.next()) { this.name=rs.getString("name"); this.birth=rs.getString("birth"); this.sex=rs.getString("sex").charAt(0); } else { throw new EJBException("this bean don't exist :id:"+id); } } catch(Exception ex) { ex.printStackTrace(); } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex) { } dab=null; } } } /** l ejbPassivate l bean處於非活躍狀態時,應做的操作 */ public void ejbPassivate() { id=null; } /** * ejbPostCreate */ public void ejbPostCreate() throws javax.ejb.CreateException { } /** l ejbRemove l 刪除bean,永久刪除,不常用 */ public void ejbRemove() throws javax.ejb.RemoveException { DataAccessBean dab=new DataAccessBean(); Try { //connection dab.set("delete from template1 where id =? "); dab.setString(1,id); //execute:insert into template (id) values ? dab.executeUpdate(); } catch(Exception ex) { ex.printStackTrace(); throw new RemoveException("the class of TDomain:"+ex.toString()); } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex){ } dab=null; } } } /** l ejbStore l bean處於非活躍狀態時,應將數據項中的指裝入數據庫或其他可持續的地方 */ public void ejbStore() { DataAccessBean dab=new DataAccessBean(); If(isModified){ Try { //connection dab.setSql("update template1 set name =?,birth =?,sex =? where id =? "); dab.setString(1,name); dab.setString(2,birth); dab.setString(3,String.valueOf(sex)); dab.setString(4,id); //execute:insert into template (id) values ? dab.executeUpdate(); isModified=false; } catch(Exception ex) { ex.printStackTrace(); } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex) { } dab=null; } } } } /** l domains l 數據項,盡量設立缺省值 */ String name="guest"; String id; String birth="20000000"; char sex='f'; boolean isModifIEd=false; /** l ejbPostCreate l 與ejbCreate同時存在,,用於創建, l ejbCreate用於創建新項,ejbPostCreate用與創建後應做的事,有先後順序 l 與Home接口的Create方法對應, l 注意三者的異常和返回值,一般來說,ejbCreate返回其主鍵類,Create返回其遠端接口,兩者除了Create的RemoteException異常外,必須用於相同的異常,其次CreateException必須有 l ejbPostCreate一般沒有返回值,沒有異常 l 由於container的調用順序為ejbCreate,ejbPostCreate,Store,所以在生成新項時先要對每一個沒有缺省值的數據項設值,特別時主鍵,要注意此點,否則會出現無法想象的結果 * @param id String * @param name String * @param birth String * @param sex char */ public void ejbPostCreate(String id,String name,String birth,char sex) { } /** * ejbCreate * @param id String * @param name String * @param birth String * @param sex char * @return TDomainKey */ public TDomainKey ejbCreate(String id,String name,String birth,char sex) throws CreateException { DataAccessBean dab=new DataAccessBean(); Try { //set values this.id=id; this.name=name; this.birth=birth; this.sex=sex; //connection dab.setSql("insert into template1 (id) values (?) "); dab.setString(1,id); //execute:insert into template (id) values ? dab.executeUpdate(); return new TDomainKey(id); } catch(Exception ex) { ex.printStackTrace(); throw new CreateException("the class of TDomain :"+ex.toString()); } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex) { } dab=null; } } } /** * Gets the name * @return Returns a String */ public String getName() { return name; } /** * Sets the name * @param name The name to set */ public void setName(String name) { if(this.name.equals(name))isModified=true; this.name = name; } /** * Gets the birth * @return Returns a String */ public String getBirth() { return birth; } /** * Sets the birth * @param birth The birth to set */ public void setBirth(String birth) { if(this.birth.equals(birth))isModified=true; this.birth = birth; } /** * Gets the sex * @return Returns a char */ public char getSex() { return sex; } /** * Sets the sex * @param sex The sex to set */ public void setSex(char sex) { if(this.sex.equals(sex))isModifIEd=true; this.sex = sex; } /** l generic Attribute method l 此處用於修改數據,采用了generic Attribute method 模式,主要是為了減少遠端調用並提供了通用的調用方式, * @param Attri hashMap * */ public void changeValue(HashMap attri) { if(attri.containsKey("name")) { setName((String)attri.get("name")); } if(attri.containsKey("sex")) { setSex(((String)attri.get("sex")).charAt(0)); } if(attri.containsKey("birth")) { setBirth((String)attri.get("birth")); } } } d、TdomainKey 在java perspective中,修改TdomainKey 將主鍵加入到代碼中 /** * Key class for Entity Bean: TDomain */ public class TDomainKey implements java.io.Serializable { static final long serialVersionUID = 3206093459760846163L; /** * primary key */ String id; /** * Creates an empty key for Entity Bean: TDomain */ public TDomainKey() { } /** * Creates an empty key for Entity Bean: TDomain */ public TDomainKey(String id) { this.id=id; } /** * Returns true if both keys are equal. */ public boolean equals(java.lang.Object otherKey) { if (otherKey instanceof TDomainKey) { TDomainKey o = (TDomainKey)otherKey; return (id.equals(o.getValue())); } return false; } /** * Return value of primarykey */ public String getValue() { return id; } /** * Returns the hash code for the key. */ public int hashCode() { return (id.hashCode()); } } 部署代碼,到J2EE perspective,在ejb項目中,如圖操作 部署完成後既可運行發布,並調試代碼,需注意的是,服務器可用兩種方式啟動,一種是正常方式,一種是調試方式(可單步跟蹤)調試需注意:分三步:每個ejb的模塊調試,集成調試,系統調試(由專人負責)模塊調試的環境為:客戶端測試環境,如圖其調用方法和程序調用相同綁定name:其步驟,打開ejb extension editor,其綁定的名稱為 name:ejb/Tdomain 見圖 cmp 首先生成一個enterprise bean,bean type定為cmp,然後加入數據項(需要錄入數據庫的數據項)如圖:(注意必須設置主鍵) 輸入代碼: a、 TdepartmentHome import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; import javax.ejb.FinderException; /** * Home interface for Enterprise Bean: TDepartment */ public interface TDepartmentHome extends javax.ejb.EJBHome { /** * Creates an instance from a key for Entity Bean: TDepartment */ public TDepartment create(java.lang.String id) throws javax.ejb.CreateException, java.rmi.RemoteException; /** * Finds an instance using a key for Entity Bean: TDepartment */ public TDepartment findByPrimaryKey(TDepartmentKey primaryKey) throws javax.ejb.FinderException, java.rmi.RemoteException; /** l Finds an instance using name for Entity Bean: TDepartment l 注意在設計cmp時,對於finder方法,必須在home寫出,然後在ejb 的擴展editor編寫其sql語句,而不象bmp需要在bean class中寫對應bean 方法,這一點需要注意 */ public Collection findByName(String name) throws javax.ejb.FinderException, java.rmi.RemoteException; /** * ejbCreate * @param id String * @param name String * @param director String * @return TDepartmentKey */ public TDepartment create(String id,String name,String director) throws CreateException, java.rmi.RemoteException; } b、 TDepartmentBean import java.rmi.RemoteException; import java.util.HashMap; import java.util.List; import java.util.Vector; import javax.ejb.CreateException; import javax.ejb.EntityBean; import javax.ejb.EntityContext; import javax.ejb.FinderException; import javax.ejb.RemoveException; import com.ibm.ivj.ejb.associations.interfaces.Link; /** * Bean implementation class for Enterprise Bean: TDepartment */ public class TDepartmentBean implements javax.ejb.EntityBean { private javax.ejb.EntityContext myEntityCtx; /** * Implemetation field for persistent attribute: id */ public java.lang.String id; /** * Implemetation field for persistent attribute: name */ public java.lang.String name; /** * Implemetation fIEld for persistent attribute: director */ public java.lang.String director; /** * getEntityContext */ public javax.ejb.EntityContext getEntityContext() { return myEntityCtx; } /** * setEntityContext */ public void setEntityContext(javax.ejb.EntityContext ctx) { myEntityCtx = ctx; } /** * unsetEntityContext */ public void unsetEntityContext() { myEntityCtx = null; } /** * ejbActivate */ public void ejbActivate() { _initLinks(); } /** * ejbCreate method for a CMP entity bean. */ public TDepartmentKey ejbCreate(java.lang.String id) throws javax.ejb.CreateException { _initLinks(); this.id = id; return null; } /** * ejbLoad */ public void ejbLoad() { _initLinks(); } /** * ejbPassivate */ public void ejbPassivate() { } /** * ejbPostCreate */ public void ejbPostCreate(java.lang.String id) throws javax.ejb.CreateException { } /** * ejbRemove */ public void ejbRemove() throws javax.ejb.RemoveException { try { _removeLinks(); } catch (java.rmi.RemoteException e) { throw new javax.ejb.RemoveException(e.getMessage()); } } /** * ejbStore */ public void ejbStore() { } /** * This method was generated for supporting the associations. */ protected void _initLinks() { } /** * This method was generated for supporting the associations. */ protected java.util.Vector _getLinks() { java.util.Vector links = new java.util.Vector(); return links; } /** * This method was generated for supporting the associations. */ protected void _removeLinks() throws java.rmi.RemoteException, javax.ejb.RemoveException { java.util.List links = _getLinks(); for (int i = 0; i < links.size() ; i++) { try { ((com.ibm.ivj.ejb.associations.interfaces.Link) links.get(i)).remove(); } catch (javax.ejb.FinderException e) {} //Consume Finder error since I am going away } } /** * Get accessor for persistent attribute: name */ public java.lang.String getName() { return name; } /** * Set accessor for persistent attribute: name */ public void setName(java.lang.String newName) { name = newName; } /** * Get accessor for persistent attribute: director */ public java.lang.String getDirector() { return director; } /** * Set Accessor for persistent attribute: director */ public void setDirector(java.lang.String newDirector) { director = newDirector; } /** * ejbCreate * @param id String * @param name String * @param director String * @return TDepartmentKey */ public TDepartmentKey ejbCreate(String id,String name,String director) throws CreateException { this.id=id; setName(name); setDirector(director); return null; } /** * ejbPostCreate * @param id String * @param name String * @param director String * @return TDepartmentKey */ public void ejbPostCreate(String id,String name,String director) { } /** * generic Attribute method * @param Attri hashMap * */ public void changeValue(HashMap attri) { if(attri.containsKey("name")) { setName((String)attri.get("name")); } if(attri.containsKey("director")) { setDirector((String)attri.get("director")); } } } c、 TDepartment import java.rmi.RemoteException; import java.util.HashMap; import javax.ejb.EJBObject; /** * Remote interface for Enterprise Bean: TDepartment */ public interface TDepartment extends javax.ejb.EJBObject { /** * generic Attribute method * @param Attri hashMap * */ public void changeValue(HashMap attri) throws java.rmi.RemoteException; } ejb/rdb mapping(既是將數據項映射到映射數據庫上), 生成map以後,可以查閱如下圖 在ejb editor中,finder中,填寫sql語句,一般來說可以選取Full select,或 whereclause,兩者可用標准的sql表示,對於ejbql來說,適合ejb2.0 server層用sessionbean封裝usercase,每個method就是一個usercase 注意sessionbean中的所有方法,應只屬於一個角色進入J2EE perspective,過程與建立bmp一樣,建立一個sessionbean package的命名如下: com.aostar.模塊名稱.server 其代碼如下: 1、 TserverHome /** * Home interface for Enterprise Bean: Tserver */ public interface TserverHome extends javax.ejb.EJBHome { /** * Creates a default instance of Session Bean: Tserver */ public Tserver create() throws javax.ejb.CreateException, java.rmi.RemoteException; } 2、 Tserver import java.rmi.RemoteException; import javax.ejb.EJBObject; import javax.sql.RowSet; /** * Remote interface for Enterprise Bean: Tserver */ public interface Tserver extends javax.ejb.EJBObject { /** * * jdbc for reading * readData * @param birth1 String * @param birth2 String */ public RowSet readData(String birth1,String birth2) throws java.rmi.RemoteException; /** * * create * @param id String * @param name String * @param birth String * @param sex char */ public void create(String id,String name ,String birth,String sex) throws Exception, java.rmi.RemoteException; } 3、 TserverBean import java.sql.Connection; import java.sql.PreparedStatement; import java.util.HashMap; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.RowSet; import sun.jdbc.rowset.CachedRowSet; import com.aostor.persistence.Database; /** * Bean implementation class for Enterprise Bean: Tserver */ public class TserverBean implements javax.ejb.SessionBean { private javax.ejb.SessionContext mySessionCtx; /** * getSessionContext */ public javax.ejb.SessionContext getSessionContext() { return mySessionCtx; } /** * setSessionContext */ public void setSessionContext(javax.ejb.SessionContext ctx) { mySessionCtx = ctx; } /** * ejbActivate */ public void ejbActivate() { } /** * ejbCreate */ public void ejbCreate() throws javax.ejb.CreateException { } /** * ejbPassivate */ public void ejbPassivate() { } /** * ejbRemove */ public void ejbRemove() { } /** l getTDomainHome() l 取得domain層的home接口 */ public TDomainHome getTDomainHome() throws Exception { Context cxt=null; Try { cxt=new InitialContext(); return (TDomainHome)cxt.lookup("ejb/TDomain"); }catch(Exception ex) { ex.printStackTrace(); throw new Exception("the class of Tserver :"+ex.toString()); } finally { if(cxt!=null) { try { cxt.close(); } catch(Exception ex) { } cxt=null; } } } /** * 封裝了Tdomain的修改和創建 * create * @param id String * @param name String * @param birth String * @param sex char */ public void create(String id,String name ,String birth,String sex) throws Exception { TDomainHome home=null; Try { //update,數據傳輸采用了HashMap模式 home=getTDomainHome(); TDomain domain=home.findByPrimaryKey(new TDomainKey(id)); HashMap attri=new HashMap(); attri.put("name",name); attri.put("birth",birth); attri.put("sex",sex); domain.changeValue(attri); }catch(FinderException fex) { //create try { home.create(id,name,birth,sex.charAt(0)); } catch(Exception ex) { ex.printStackTrace(); throw new Exception("the class of Tserver:"+ex.toString()); } }catch(Exception ex) { ex.printStackTrace(); throw new Exception("the class of Tserver:"+ex.toString()); } } /** l 查看多項數據 l 這裡采用了jdbc for reading模式,數據傳輸采用了Rowset模式 * jdbc for reading * readData * @param birth1 String * @param birth2 String */ public RowSet readData(String birth1,String birth2) { Connection con=null; PreparedStatement pstt=null; Try { //connection con=Database.getConnection(); pstt=con.prepareStatement("select * from template1 where birth>=? and birth<=? "); pstt.setString(1,birth1); pstt.setString(2,birth2); CachedRowSet rs=new CachedRowSet(); rs.populate(pstt.executeQuery()); rs.setTableName("template1"); return rs; } catch(Exception ex) { ex.printStackTrace(); return null; } finally { if(pstt!=null) { try { pstt.close(); } catch(Exception ex) { } pstt=null; } if(con!=null) { try { con.close(); } catch(Exception ex) { } con=null; } } } /** * * jdbc for reading * readData * @param birth1 String * @param birth2 String */ public RowSet readDatatest(String birth1,String birth2) { DataAccessBean dab=new DataAccessBean(); Try { dab.setSql("select * from template1 where birth>=? and birth<=? "); dab.setString(1,birth1); dab.setString(2,birth2); CachedRowSet rs=new CachedRowSet(); rs.populate(dab.executeQuery()); rs.setTableName("template1"); return rs; } catch(Exception ex) { ex.printStackTrace(); return null; } finally { if(dab!=null) { try { dab.close(); } catch(Exception ex) { } dab=null; } } } } 注意:由於遠端調用,要穿越防火牆,所以數據傳輸非常重要,可能會影響了整個系統性能,一般采用以下幾個模式: hashmap模式:用HashMap來傳遞,見上面代碼 DTO對象:DTO為數據傳輸對象,是序列化的,可在層與層之間的遠端調用中傳遞,一般用於多行數據傳遞,可用於頁面控制,需自己按需編寫。 RowSet:多項數據傳輸,見上面代碼 XML:多項數據傳輸,可用於頁面控制部署如domain層,調試如domain層 application 層] 作用:隔離ejb與JSP或者客戶端程序,提供參數檢驗功能 businessdelegate 隔離表現層和server層,提供與server相同的方法,可增加數據類型檢測了,一些論證此處使用的時businessdelegate模型,一種在J2EE設計中最常用的模式,類似於模式設計中proxy模式在java perspective中建立一個package,在其web項目的source目錄下 com.aostar.模塊名稱.businessdelegate 建立一個class,Tbusiness 代碼如下: package com.aostar.delegate; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.RowSet; import Tserver; import TserverHome; public class Tbuseness { /** l getTDomainHome() l 得到遠端 l 如果存在大量的訪問時,需要做一個專門的HomeFactory,用於控制和管理各個sessionBean的Home接口,並且它統一管理Home接口,並將businessdelegate和jininame隔離,因為jininame本身變動較大,這樣HomeFactory很大程度的減少了代碼修改,增加了代碼的可復用性,代碼如下 */ public Tserver getTserver() throws Exception { try { HomeFactory factory=HomeFactory.builder(); TserverHome home=(TserverHome)factory.findBy(TserverHome.class); return home.create(); } catch(Exception ex) { ex.printStackTrace(); throw ex; } } /** * update and create * @param id String * @param name String * @param birth String * @param sex String */ public void updateAndCreate(String id,String name ,String birth,String sex) throws Exception { //check data if(sex.length()!=1 && birth.length()!=8) { throw new Exception("error data formation"); } //execute try { Tserver delegate=getTserver(); delegate.create(id,name,birth,sex); } catch(Exception ex) { ex.printStackTrace(); throw new Exception("System error"); } } /** * showData * @param birth1 String * @param birth2 String * @return RowSet */ public RowSet showData(String birth1,String birth2) throws Exception { //check data if(birth1.length()!=8 && birth2.length()!=8) { throw new Exception("error data formation"); } //execute RowSet re=null; Try { Tserver delegate=getTserver(); if((re=delegate.readData(birth1,birth2))==null) { throw new Exception("returnvalue is null"); } } catch(Exception ex) { ex.printStackTrace(); throw new Exception("System error"); } return re; } } 在編譯之前,需要導入所需ejb的包(包括Home,遠端接口類,和一些成生的客戶端調用class,考慮到儲存空間現今已不是問題,可整個的打包),如圖在web perspective中,左鍵,出現下拉以後,點擊propertIEs進入相應界面 當然還有一種方式,就是在java build path 中project加入ejb項目既可,這是常用方法 備注:在項目中需要加入其他外面的包,比如說其他廠商開發的,如例子中引用了sun公司的RowSet包,方法於前一種類似,需要加入變量,但如果是ejb項目加包還需要包存放在運行目錄中com.ibm.etools.websphere.runtime HomeFactory 用於大型的項目,采用了singleton model 和 flightweight model相應的原因如上述代碼如下: package com.aostar.home; import java.util.HashMap; import java.util.List; import java.util.Arrays; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import TserverHome; import com.aostar.exception.HomeFactoryCreateException; public class HomeFactory { //singleton model static HomeFactory factory=null; //flightweight model static HashMap map=null; final static String[] jdbcName={"ejb/Tserver"}; final static Class[] classes={TserverHome.class}; final static int COUNT=1; /** * constructor */ private HomeFactory() throws HomeFactoryCreateException { map=new HashMap(); Context ctx=null; Try { ctx=new InitialContext(); List jdbcList=Arrays.asList(jdbcName); for(int i=0;i<jdbcList.size();i++) { Object obj=ctx.lookup((String)jdbcList.get(i)); obj=PortableRemoteObject.narrow(obj,classes); map.put(classes,obj); } } catch(Exception ex) { ex.printStackTrace(); throw new HomeFactoryCreateException("home factory can't start"); } finally { if(ctx!=null) ` { try { ctx.close(); } catch(Exception ex) { } ctx=null; } } } /** * builder */ static public HomeFactory builder() throws Exception { if(factory==null) { //try 15 times boolean flag=true; int i=1; while(flag && i<=15) { try { factory=new HomeFactory(); flag=false; } catch(HomeFactoryCreateException ex) { } i++; } if(flag){ throw new Exception("System error"); } } return factory; } /** * */ public Object findBy(Class ref) throws Exception { if(map.containsKey(ref)) { return map.get(ref); } else { throw new Exception("this home don't exist"); } } } 一些Java servlet presentation 在其web perspective中,web Application JSp和一些CSS,xsl(用於XML)等以下為例子JSP:
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved