EJB3.0 + Struts小試
一直想做一個(gè) EJB3.0 結(jié)合 Struts 的例子,前天剛好從網(wǎng)上找到了一個(gè)相關(guān)的 Tutorial ,于是小試了一把,雖然只是完成了 Tutorial 上的一小點(diǎn)功能,但是感覺還是有必要寫下來,免得以后還要再去看別人的Tutorial 。
開發(fā)環(huán)境:??
??? Eclipse3.2 + WTP1.5 , JBoss 4.0.3SP1 , jboss-EJB-3.0_RC5-PFD,MySQL5.0.16
? 環(huán)境的搭建可以參考我的學(xué)習(xí)筆記:
??? EJB 3.0 學(xué)習(xí)筆記——準(zhǔn)備工作 : http://www.aygfsteel.com/felicity/archive/2006/03/26/37510.html
??? EJB 3.0 學(xué)習(xí)筆記—— Entity Bean : http://www.aygfsteel.com/felicity/archive/2006/05/20/47229.html
??? 在MySQL? 中新建數(shù)據(jù)庫ejbstruts,在JBoss的all\deploy下新建ejbstruts-ds.xml,其內(nèi)容如下:
<!-- ?$Id:?mysql-ds.xml,v?1.3.2.1?2004/12/01?11:46:00?schrouf?Exp?$? -->
<!-- ??Datasource?config?for?MySQL?using?3.0.9?available?from:
http://www.mysql.com/downloads/api-jdbc-stable.html?
--> ?
< datasources >
?? < local-tx-datasource >
???? < jndi-name > EJBStrutsDS </ jndi-name >
???? < connection-url > jdbc:mysql://localhost:3306/ejbstruts </ connection-url >
???? < driver-class > com.mysql.jdbc.Driver </ driver-class >
???? < user-name > test </ user-name >
???? < password ></ password >
???? < exception-sorter-class-name > org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </ exception-sorter-class-name >
???? <!-- ?sql?to?call?when?connection?is?created
????<new-connection-sql>some?arbitrary?sql</new-connection-sql>
?????? -->
???? <!-- ?sql?to?call?on?an?existing?pooled?connection?when?it?is?obtained?from?pool?
????<check-valid-connection-sql>some?arbitrary?sql</check-valid-connection-sql>
?????? --> ?
???? <!-- ?corresponding?type-mapping?in?the?standardjbosscmp-jdbc.xml?(optional)? -->
???? < metadata >
??????? < type-mapping > mySQL </ type-mapping >
???? </ metadata >
?? </ local-tx-datasource >
</ datasources > ?
新建工程:
?最后的程序會(huì)打包成一個(gè)EAR部署到JBoss,所以在Eclipse中創(chuàng)建三個(gè)新的工程:
??1、EAR Project:EJBStruts???????
選好 Target Runtiem為 JBoss后Finish。
?2、Java EJB3 Project: EJBStrutsEJB
由于WTP1.5還不支持EJB3.0的直接創(chuàng)建,這里新建的是一個(gè)普通的Java Project,之后會(huì)用ant來創(chuàng)建ejb jar。
由于是普通的Java Project,之后直接Finish好了。
?3、? WEB Project:EJBStrutsWeb
這里可以將工程添加到前面建好的EAR中。
EJB模塊
向EJBStrutsEJB工程中導(dǎo)入U(xiǎn)ser Library:EJB3_JBoss
新建兩個(gè)EJB Entities:Book 和Customer
?*?Book.java
? */
package ?com.lzy.library.domain;
import ?java.io.Serializable;?
import ?javax.persistence.Column;
import ?javax.persistence.Entity;
import ?javax.persistence.GeneratedValue;
import ?javax.persistence.GenerationType;
import ?javax.persistence.Id;
import ?javax.persistence.JoinColumn;
import ?javax.persistence.ManyToOne;
import ?javax.persistence.Table;?
@Entity
@Table(name? = ? " BOOK " )
public ? class ?Book? implements ?Serializable?{
? /**
??*?
?? */
? private ? static ? final ? long ?serialVersionUID? = ? 1L ;?
? private ?Integer?id;?
? private ?String?title;?
? private ?String?author;?
? private ?Customer?customer;?
? public ?Book()?{
?? super ();
?}?
? public ?Book(Integer?id,?String?title,?String?author)?{
?? super ();
?? this .id? = ?id;
?? this .title? = ?title;
?? this .author? = ?author;
?}?
?@Column(name? = ? " AUTHOR " )
? public ?String?getAuthor()?{
?? return ?author;
?}?
? public ? void ?setAuthor(String?author)?{
?? this .author? = ?author;
?}?
?@Id
?@GeneratedValue(strategy? = ?GenerationType.AUTO)
?@Column(name? = ? " ID " )
? public ?Integer?getId()?{
?? return ?id;
?}?
? public ? void ?setId(Integer?id)?{
?? this .id? = ?id;
?}?
?@Column(name? = ? " TITLE " )
? public ?String?getTitle()?{
?? return ?title;
?}?
? public ? void ?setTitle(String?title)?{
?? this .title? = ?title;
?}?
?@ManyToOne
?@JoinColumn(name? = ? " CUSTOMER_ID " )
? public ?Customer?getCustomer()?{
?? return ?customer;
?}?
? public ? void ?setCustomer(Customer?customer)?{
?? this .customer? = ?customer;
?}?
?@Override
? public ?String?toString()?{
?? return ? " Book:? " ? + ?getId()? + ? " ?Title? " ? + ?getTitle()? + ? " ?Author? "
???? + ?getAuthor();
?}?
}
?











































































































在工程下新建
META-INF目錄,該目錄下新建persistence.xml文件。












然后新建DAO接口BookDao(Local)和CustomerDao(Local),以及各自的實(shí)現(xiàn)BookDaoImpl(Stateless)和CustomerDaoImpl(Stateless),在后面的Web工程中,將通過這兩個(gè)DAO接口進(jìn)行數(shù)據(jù)庫操作。由于這部分可供參考的例子很多,略去源碼。
新建文件builder.xml














































































Ant build后將可以看到EJBStrutsEJB.jar。
Web(struts)模塊:
這部分更加簡單,完成一個(gè)最簡單的功能:向數(shù)據(jù)庫中添加測(cè)試數(shù)據(jù)。
只需要在相應(yīng)的Action類里得到DAO,然后操作數(shù)據(jù)庫。
首先將EJBStrutsEJB中的EJBStrutsEJB.jar添加到工程的build path 中:
然后創(chuàng)建相應(yīng)的Action類:
?*?Common.java
? */
package ?com.lzy.library.web.action;?
/**
?*? @author ?rouserli
?*
? */
public ? class ?Common?{
? private ?Common(){
?}
? public ? static ? final ?String?JNDI_PREFIX? = ? " LearnEJB3/ " ;
// ??forward?used?as?success
? public ? static ? final ?String?SUCCESS? = ? " success " ;
// ??forward?used?for?failure
? public ? static ? final ?String?FAILURE? = ? " failure " ;
// ??forward?used?for?self
? public ? static ? final ?String?SELF? = ? " self " ;
// ??session?key?for?customer
? public ? static ? final ?String?CUSTOMER_KEY? = ? " customer_key " ;
// ??session?key?for?cart
? public ? static ? final ?String?CART_KEY? = ? " cart " ;
}
?*?
? */
package ?com.lzy.library.web.action;?
import ?java.util.Random;?
import ?javax.naming.Context;
import ?javax.naming.InitialContext;
import ?javax.naming.NamingException;
import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.http.HttpServletResponse;?
import ?org.apache.struts.action.Action;
import ?org.apache.struts.action.ActionForm;
import ?org.apache.struts.action.ActionForward;
import ?org.apache.struts.action.ActionMapping;?
import ?com.lzy.library.dao.BookDao;
import ?com.lzy.library.dao.CustomerDao;
import ?com.lzy.library.dao.impl.BookDaoImpl;
import ?com.lzy.library.dao.impl.CustomerDaoImpl;
import ?com.lzy.library.domain.Book;
import ?com.lzy.library.domain.Customer;?
/**
?*? @author ?rouserli
?*?
? */
public ? class ?CreateSampleDataAction? extends ?Action?{?
?@Override
? /**
??*?Method?execute
??*?
??*? @param ?mapping
??*? @param ?form
??*? @param ?request
??*? @param ?response
??*? @return ?ActionForward
?? */
? public ?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,
???HttpServletRequest?request,?HttpServletResponse?response)?{
??System.out.println( " CreateSampleData " );
??BookDao?bookDao;
??CustomerDao?customerDao;
??Random?r? = ? new ?Random();
?? try ?{
???Context?context? = ? new ?InitialContext();
???
???System.out.println( " Look?up?BookDaoImpl: " + BookDaoImpl.LocalJNDIName);
???System.out.println( " Look?up?CustomerDaoImpl: " + CustomerDaoImpl.LocalJNDIName);
???bookDao? = ?(BookDao)?context.lookup( " EJBStruts/ " ? + ?BookDaoImpl.LocalJNDIName);
???customerDao? = ?(CustomerDao)?context
?????.lookup( " EJBStruts/ " ? + ?CustomerDaoImpl.LocalJNDIName);
??}? catch ?(NamingException?e)?{
???e.printStackTrace();
??? throw ? new ?RuntimeException(e);
??}
??System.out.println( " to?return?from?action " );
??
??Book?book? = ? new ?Book( null ,? " EJB?3?Developer? " ? + ?r.nextInt( 100 ),
???? " Sebastian " );
??bookDao.save(book);
??
??Customer?customer? = ? new ?Customer( null ,? " Sebastian? " ? + ?r.nextInt( 100 ));
??
??
??customerDao.save(customer);
?
?? return ?mapping.findForward(Common.SUCCESS);
?}?
}
在src文件夾中新建
jndi.properties:



頁面文件:
welcom.jsp

































EAR工程打包、部署、運(yùn)行:
修改application.xml如下:

















確認(rèn)EAR工程的模塊依賴無誤后Export出ear包。
將導(dǎo)出的EJBStruts.ear文件拷到JBoss的all/deploy目錄下,以all模式啟動(dòng)JBoss。
確認(rèn)啟動(dòng)無誤之后可以在你的瀏覽器中打開welcom.jsp,點(diǎn)擊鏈接,如果執(zhí)行正確,就會(huì)發(fā)現(xiàn)數(shù)據(jù)已經(jīng)寫入數(shù)據(jù)庫中。
Reference:
??? EJB 3 Struts Framework Integration Tutorial, by Sebastian Hennebrueder
??? OReilly.Enterprise.JavaBeans.3.0.5th.Edition, by Bill Burke , Richard Monson-Haefel
posted on 2006-08-11 13:52 all gone 閱讀(3069) 評(píng)論(0) 編輯 收藏 所屬分類: Java