?? 5.mht
字號:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Tutorial for building J2EE Applications using JBOSS and ECLIPSE
Date: Sun, 5 Dec 2004 11:55:11 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_NextPart_000_00F3_01C4DAC1.4635A2C0";
type="text/html"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
This is a multi-part message in MIME format.
------=_NextPart_000_00F3_01C4DAC1.4635A2C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.tusc.com.au/tutorial/html/chap5.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Tutorial for building J2EE Applications using JBOSS =
and ECLIPSE</TITLE>
<META http-equiv=3DCONTENT-TYPE content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1479" name=3DGENERATOR>
<META content=3D"Glen McCallum and Vishal Sharma" name=3DAUTHOR>
<META content=3D20030729;9513900 name=3DCREATED>
<META content=3DVishal name=3DCHANGEDBY>
<META content=3D20031118;15354000 name=3DCHANGED>
<META=20
content=3D"Tutorial for building J2EE Applications using Eclipse, =
Lomboz, Jboss and Xdoclet."=20
name=3DDESCRIPTION>
<META=20
content=3D"J2EE, Traininig,Tutorial, J2EE, Lomboz, Eclipse, Jboss, =
Xdoclet, J2EE Patterns, EJB, Servlets, JSP, BMP, CMP, Session, =
Configuration, DAO"=20
name=3DKEYWORDS>
<META content=3D"14 days" name=3Drevisit-after><!-- Meta tags --><!-- =
Copyright =A9 2003 TUSC. All rights reserved.--><!--- Header --></HEAD>
<BODY lang=3Den-US dir=3Dltr>
<FORM name=3DsearchForm action=3D../../../../search/index.php =
method=3Dpost>
<TABLE style=3D"PAGE-BREAK-BEFORE: always" cellSpacing=3D0 =
cellPadding=3D4 width=3D957=20
bgColor=3D#0a6a91 border=3D0>
<COLGROUP>
<COL width=3D154>
<COL width=3D276>
<COL width=3D21>
<COL width=3D43>
<COL width=3D119>
<COL width=3D31>
<COL width=3D50>
<COL width=3D178>
<COL width=3D14>
<TBODY>
<TR>
<TD vAlign=3Dtop width=3D154>
<P><A href=3D"http://www.tusc.com.au./contact_us.php"><IMG =
height=3D125=20
alt=3DTUSC=20
=
src=3D"http://www.tusc.com.au/tutorial/images/header/tuscJ2eeLogo.gif"=20
width=3D149 align=3Dbottom border=3D0 =
name=3DGraphic1></A></P></TD>
<TD vAlign=3Dtop width=3D276>
<P><IMG height=3D73 alt=3D"Reliable, On-Time Delivery."=20
src=3D"http://www.tusc.com.au/tutorial/images/header/reliable.gif" =
width=3D268=20
align=3Dbottom border=3D0 name=3DGraphic2><!--search table =
--></P></TD>
<TD width=3D21></TD>
<TD width=3D43>
<P><IMG height=3D9 alt=3DSearch=20
src=3D"http://www.tusc.com.au/tutorial/images/elements/search.gif" =
width=3D41=20
align=3Dbottom border=3D0 name=3DGraphic3></P></TD>
<TD width=3D119>
<P><INPUT maxLength=3D50 size=3D12 name=3DKEYWORD></P></TD>
<TD width=3D31>
<P><INPUT type=3Dimage height=3D18 width=3D29=20
src=3D"http://www.tusc.com.au/tutorial/images/btns/go.gif" =
align=3Dbottom=20
name=3Dgo><INPUT type=3Dhidden value=3DY =
name=3DSECTION_ALL></P></TD>
<TD vAlign=3Dtop width=3D50>
<P><IMG height=3D73 alt=3DSpacer=20
src=3D"http://www.tusc.com.au/tutorial/images/invis.gif" =
width=3D50=20
align=3Dbottom border=3D0 name=3DGraphic5></P></TD>
<TD vAlign=3Dtop width=3D154>
<P><A href=3D"http://www.tusc.com.au./contact_us.php"><IMG =
height=3D125=20
alt=3DTUSC=20
=
src=3D"http://www.tusc.com.au/tutorial/images/header/tuscJ2eeLogo.gif"=20
width=3D149 align=3Dbottom border=3D0 =
name=3DGraphic1></A></P></TD>
<TD width=3D14>
<P><IMG height=3D10 alt=3Dspacer=20
src=3D"http://www.tusc.com.au/tutorial/images/invis.gif" =
width=3D14=20
align=3Dbottom border=3D0 name=3DGraphic58> =
</P></TD></TR></TBODY></TABLE></FORM>
<P><!--- Header finished --><BR><BR></P>
<H2 align=3Dcenter>Tutorial for building J2EE Applications using JBOSS =
and ECLIPSE=20
<IMG height=3D2 =
src=3D"http://www.tusc.com.au/tutorial/images/redrulr5.gif"=20
width=3D775 align=3Dmiddle border=3D0 name=3DGraphic1></H2>
<P><BR><BR></P>
<H1>Chapter 5. </H1>
<P><IMG height=3D2 =
src=3D"http://www.tusc.com.au/tutorial/images/blurulr6.gif"=20
width=3D785 align=3Dmiddle border=3D0 name=3DGraphic2></P>
<H2>Creating a BMP Entity Bean</H2>
<P>This chapter describes how to create a Bean Managed Persistence (BMP) =
EJB=20
component. We will create two BMP beans, Customer and Manager, as shown =
below.=20
The Customer bean will be responsible for storing the details of =
customers of=20
MyStore. The Manager bean stores details of the manager of MyStore. Both =
beans=20
communicate with their respective tables in the database using Data =
Access=20
Objects (DAOs) named CustomerDAO and ManagerDAO respectively. </P>
<P><BR><BR></P>
<P><IMG height=3D268=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/J2EECaseStudyFlowDiag=
ram.png"=20
width=3D450 align=3Dleft border=3D0 name=3DGraphic37><BR =
clear=3Dleft><BR><BR></P>
<P>All customers have been assigned a unique customerID for housekeeping =
purposes in MyStore in addition to their username for accessing the =
services of=20
MyStore. Similarly the Manager of MyStore has been assigned a unique=20
ManagerID.</P>
<P><BR><BR></P>
<P><FONT color=3D#800000><I>Note : It is the usual practice to access =
business=20
methods of BMP beans via a session bean, which encapsulates business =
logic and=20
acts as an interface to further EJB components. In this case Customer =
and=20
Manager are accessible via by StoreAccess.</I></FONT></P>
<P><FONT color=3D#800000><I>This approach comes from a pattern called a =
Session=20
Facade, whereby enterprise beans encapsulate business logic and business =
data=20
and expose their interfaces. The session bean acts as a facade to =
encapsulate=20
the complexity of interactions with the lower-level beans. The session =
facade is=20
responsible for managing business objects and provides a uniform =
business=20
service abstraction to presentation layer clients, thereby hiding the =
business=20
object implementation in the lower-level beans.</I></FONT></P>
<P><I><FONT color=3D#800000>This tutorial uses that pattern for business =
tier=20
implementation</FONT></I>.</P>
<H3>Tasks :</H3>
<OL>
<LI>
<P>Create a BMP bean named Customer under package au.com.tusc.bmp.</P>
<LI>
<P>Create a DAO class named CustomerDAOImpl under package =
au.com.tusc.dao.</P>
<LI>
<P>Add all attributes/properties to the CustomerBean, with getter and =
setter=20
methods for each of the attributes.</P>
<LI>
<P>Add a finder method named ejbFindByPrimaryKey with the =
signature</P>
<P><B><FONT color=3D#000000>public CustomerPK</FONT> <FONT=20
color=3D#000000>ejbFindByPrimaryKey</FONT> <FONT=20
color=3D#000000>(CustomerPK</FONT> <FONT color=3D#000000>pk)</FONT> =
throws F<FONT=20
color=3D#000000>inderException</FONT> </B></P>
<LI>
<P>Add a finder method named ejbFindByUserID with the signature </P>
<P><B><FONT color=3D#000000>public CustomerPK</FONT> <FONT=20
color=3D#000000>ejbFindByUserID</FONT> <FONT color=3D#000000>(String=20
userID)</FONT> throws F<FONT color=3D#000000>inderException</FONT> =
</B></P>
<LI>
<P>Add a business method named getCustomerData with the signature</P>
<P><FONT color=3D#7f0055><B><FONT color=3D#000000>public =
CustomerData</FONT> <FONT=20
color=3D#000000>getCustomerData()</FONT></B></FONT></P>
<LI>
<P>Implement required methods in the CustomerDAOImpl class.</P>
<LI>
<P>Deploy the Customer Bean.</P>
<LI>
<P>Add a create method to the StoreAccess Bean.</P>
<P><B>public void ejbCreate() throws javax.ejb.CreateException =
</B></P>
<LI>
<P>Add a business method to the StoreAccess Bean.</P>
<P><FONT face=3D"Nimbus Roman No9 L"><B><FONT color=3D#000000>public=20
CustomerData</FONT> <FONT color=3D#000000>getCustomerData(String =
userID)=20
</FONT></B></FONT></P>
<LI>
<P>Create a test client named SessionBMPClient under package=20
au.com.tusc.client. </P>
<LI>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L">Run your =
client and=20
test the bean.</FONT></FONT></P></LI></OL>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create the Customer =
BMP=20
Entity Bean :</B></H3>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P><B>Go To Package Explorer > Expand Mystore (project) node > =
select src,=20
right click and a menu will pop up.</B></P>
<P><B>On the pop up menu > New > Lomboz EJB Creation =
Wizard.</B></P>
<P><B>Enter package name au.com.tusc.bmp, bean name Customer and select =
bean=20
type as Bean Manged Entity > Finish.</B></P>
<P><B>This will create a package named au.com.tusc.bmp under src and=20
CustomerBean under that package as shown below.</B></P>
<P><IMG height=3D329=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBmp.png" =
width=3D386=20
align=3Dleft border=3D0 name=3DGraphic3><BR clear=3Dleft><FONT =
color=3D#800000><I>Note: It=20
will generate the bean name, jndi-name and type of bean in file. Also, =
the word=20
'Bean' is appended to the name of the file.</I></FONT></P>
<P align=3Dleft>As we can see from the figure above it has created a =
class level=20
tag <B>@ejb.bean</B>, which has assigned the bean type, its name and its =
JNDI=20
name which will be generated in the Home interface. This tag will also =
generate=20
deployment descriptors in ejb-jar.xml and jboss.xml file once you =
generate your=20
EJB classes.</P>
<P>Now we are going to generate all the interfaces including Home, =
Remote, DAO=20
and other helper classes. We don't need to specify any tags in =
ejbGenerate.xml=20
as we have already set up that for the MyStoreMgr EJB module in chapter =
3.</P>
<P><B>Go to node CustomerBean under au.com.tusc.bmp > LombozJ2EE > =
Add EJB=20
to Module > Select MyStoreMgr > Ok.</B></P>
<P><B>Go to MyStoreMgr node > LombozJ2EE > Generate EJB =
classes.</B></P>
<P><FONT color=3D#800000><I>Note: All these steps are covered in =
previous chapters=20
(chapter 3 and 1) so please refer to those sections if you need further =
details.=20
</I></FONT></P>
<P>Now let's see what files are generated by Xdoclet.</P>
<P>As shown below there are two new files named CustomerData and =
CustomerPK in=20
addition to what we had for our session beans. We also have a =
CustomerBMP which=20
extends our CustomerBean class. The remaining files should be familiar =
from our=20
work with session beans.</P>
<P><IMG height=3D239=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerSrcWithoutDAO=
.png"=20
width=3D215 align=3Dleft border=3D0 name=3DGraphic4><BR =
clear=3Dleft><B>As discussed in=20
chapter 3 regading the various ejbDoclet tags used in ejbGenerate.xml, =
to=20
generate these classes, CustomerData and CutomerPk are generated by =
following=20
tags as shown below in this snippet from ejbGenerate.xml:</B></P>
<P><B>The relevant tags are <dataobject/> and =
<entitypk/>.</B></P>
<P><IMG height=3D276=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/ejbGeneratePkTag.png"=
=20
width=3D268 align=3Dleft border=3D0 name=3DGraphic31><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note: There is no CustomerDAO class, as we haven't =
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -