📄 3.mht
字号:
class=3Dau.com.tusc.session.StoreAccessDAO</FONT></FONT></FONT>
<FONT color=3D#3f5fbf> <FONT size=3D3><FONT =
face=3DSans>impl-class=3Dau.com.tusc.dao.StoreAccessDAOImpl</FONT></FONT>=
</FONT></PRE>
<P><B>Other files of interest which are generated are ejb-jar.xml =
and=20
jboss.xml under MyStoreMgr/META-INF. </B></P></TD>
<TD width=3D"30%">
<P><IMG height=3D518=20
=
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/GeneratedEJBFilesan=
dDescriptors.png"=20
width=3D270 align=3Dleft border=3D0 name=3DGraphic8><BR=20
clear=3Dleft><BR></P></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<TABLE cellSpacing=3D4 cellPadding=3D2 width=3D"100%" border=3D0>
<COLGROUP>
<COL width=3D106>
<COL width=3D150>
<THEAD>
<TR vAlign=3Dtop>
<TD width=3D"42%">
<P><B>As shown in the figure on the right, a few descriptors are =
generated=20
in the ejb-jar.xml file.</B></P>
<P><B>These descriptors are generated by the following tag =
declared in the=20
StoreAccesBean file.</B></P><PRE style=3D"MARGIN-BOTTOM: =
0.2in"><FONT color=3D#3f5fbf><FONT face=3DSans><FONT size=3D3>@ejb.bean =
name =3D"StoreAccess"</FONT></FONT></FONT>
<FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>jndi-name=3D"StoreAccessBean"</FONT></FONT></FONT>
<FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>type=3D"Stateless" </FONT></FONT></FONT></PRE>
<P><B>This tag is added by Lomboz's bean creation =
wizard.</B></P></TD>
<TD width=3D"58%">
<P align=3Dright><IMG height=3D431=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/ejb-jar.png" =
width=3D501=20
align=3Dleft border=3D0 name=3DGraphic9><BR =
clear=3Dleft><BR></P></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<P>T<B>his tag also generates the following descriptors in jboss.xml as =
shown in=20
the code snippet below.</B></P>
<P><IMG height=3D477=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/jboss.png" =
width=3D551=20
align=3Dleft border=3D0 name=3DGraphic10><BR clear=3Dleft><B>So now we =
know which tags=20
are responsible for generating classes, interfaces and =
descriptors</B>.</P>
<H3>Add Business Method :</H3>
<P><B>Next step is to add a business method in the bean.</B></P>
<P><B>Go to StoreAccesBean > Right click > Select New on pop up =
menu >=20
Select Lomboz Ejb Method Wizard.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D250=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/CreateEJBMethod.png=
"=20
width=3D697 align=3Dleft border=3D0 name=3DGraphic46><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add a business method with the =
following=20
signature: public String loginUser (String username, String =
password).</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Select Method Type as Business and =
Interface as=20
Remote as shown in the figure below..</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D329=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/AddBusinessBMethod.=
png"=20
width=3D502 align=3Dleft border=3D0 name=3DGraphic6><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>This wizard generates a loginUser =
method in our=20
bean class, with the method level tag '@ejb.interface' shown =
below.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D103=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/SessionBeanInterfac=
eTag.png"=20
width=3D359 align=3Dleft border=3D0 name=3DGraphic22><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>This tag is responsible for =
generating this=20
method in the Remote Interface (in this case it is StoreAccess which =
will be=20
created once you generate your classes). This tag is covered later on in =
this=20
chapter. </B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<TABLE cellSpacing=3D4 cellPadding=3D4 width=3D"100%" border=3D0>
<COLGROUP>
<COL width=3D134>
<COL width=3D122>
<THEAD>
<TR vAlign=3Dtop>
<TD width=3D"52%">
<P align=3Dleft><B>Now, This business method needs to invoke a =
method on the=20
DAO, which will communicate with the database.</B></P>
<P align=3Dleft><B>Therefore we add another tag on this method, so =
that a=20
method with this signature is generated in DAO interface which we =
can=20
implement in the DAOImpl class. Then this business method can =
invoke the=20
method in DAOImpl class to get the desired result.</B></P><PRE =
style=3D"MARGIN-BOTTOM: 0.2in"><FONT color=3D#3f5fbf><FONT =
face=3DSans><FONT size=3D3>@dao.call =
name=3D"loginUser"</FONT></FONT></FONT></PRE>
<P><B>So add this tag at the method level as shown in the figure =
at=20
right.</B></P>
<P><B>Now generate your EJB classes again as shown in the steps we =
went=20
through earlier.</B></P>
<P><FONT color=3D#800000><I>Note: OK, OK! For reference these are =
the steps=20
you have to follow.</I></FONT></P>
<P><FONT color=3D#800000><I>Expand 'MyStoreMgr' node under =
'MyStore' Project=20
in Package Explorer.</I></FONT></P>
<P><FONT color=3D#800000><I>Right click and a pop up menu will=20
appear.</I></FONT></P>
<P><FONT color=3D#800000><I>Go to Lomboz J2EE > Generate EJB=20
Classes.</I></FONT></P></TD>
<TD width=3D"48%">
<P><IMG height=3D413=20
=
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/looinUserAddDAOTag.=
png"=20
width=3D421 align=3Dleft border=3D0 name=3DGraphic15><BR=20
clear=3Dleft><BR></P></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<P><B>After generating the classes, we look at first the generated DAO =
interface=20
and then the generated Session Class.</B></P>
<P><B>In StoreAcessDAO two methods are generated.</B></P>
<P><B>1. init() by default.</B></P>
<P><B>2. loginUser(), generated by tag shown below. </B></P><PRE =
style=3D"MARGIN-BOTTOM: 0.2in"><FONT color=3D#3f5fbf><FONT =
face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2>@dao.call =
name=3D"loginUser"</FONT></FONT></FONT></PRE>
<P align=3Dleft><IMG height=3D280=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessDAO.png"=
=20
width=3D536 align=3Dleft border=3D0 name=3DGraphic17><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note: Please do not edit any class generated by =
Xdoclect<FONT=20
face=3DSans>.</FONT></I></FONT></P>
<P><B>In StoreAcessSession two methods of our interest are </B></P>
<P><B>1. getDAO() creates instance of DAOImpl calss.</B></P>
<P><B>2. loginUser(), calls loginUser method in DAOImpl class, which we =
have to=20
implement. </B></P>
<P><B>Code snippet from 'StoreAccessSession'.</B></P>
<P><IMG height=3D456=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessSession.=
png"=20
width=3D542 align=3Dleft border=3D0 name=3DGraphic4><BR =
clear=3Dleft><BR><BR></P>
<H3>Implement DAO Interface :</H3>
<P><B>Now, we will implement methods in the StoreAccessDAOImpl =
class.</B></P>
<TABLE cellSpacing=3D4 cellPadding=3D4 width=3D"100%" border=3D0>
<COLGROUP>
<COL width=3D116>
<COL width=3D140>
<THEAD>
<TR vAlign=3Dtop>
<TD width=3D"45%">
<P align=3Dleft><B>First import the following packages.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>javax.naming.InitialContext;</FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>javax.sql.DataSource;</FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>java.sql.Connection;</FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>java.sql.PreparedStatement;</FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>java.sql.ResultSet;</FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#000000><FONT =
face=3DSans><FONT=20
size=3D3>java.sql.SQLException;</FONT></FONT></FONT></P>
<P><B>Change your class declaration so that StoreAccessDAOImpl =
implements=20
StoreAccessDAO.</B></P>
<P><B>Add a field to store the JDBC resource factory =
reference.</B></P>
<P><FONT color=3D#000000><FONT face=3DSans><FONT size=3D3><B><FONT =
color=3D#7f0055>private</FONT></B> DataSource=20
jdbcFactory;</FONT></FONT></FONT></P>
<P><B>In init() method, locate the reference "jdbc/DefaultDS" =
using the=20
JNDI API, and store the reference in variable jdbcFactory.</B></P>
<P><B>Lookup string is </B><FONT face=3DSans><FONT=20
color=3D#2a00ff>"java:comp/env/jdbc/DefaultDS".</FONT></FONT></P>
<P>Code Snippet is shown in the figure on the right.</P>
<P>Now add the required code in loginUser().</P></TD>
<TD width=3D"55%">
<P><IMG height=3D585=20
=
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessDAOImplI=
nit.png"=20
width=3D512 align=3Dright border=3D0 name=3DGraphic18><BR=20
clear=3Dright><BR></P></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<TABLE cellSpacing=3D4 cellPadding=3D4 width=3D"100%" border=3D0>
<COLGROUP>
<COL width=3D94>
<COL width=3D162>
<THEAD>
<TR vAlign=3Dtop>
<TD width=3D"37%">
<P><B>In method loginUser(), first get the connection to the =
database=20
using the jdbcFactory.</B></P>
<P><B>Create a SQL statement which searches for userid in the =
table=20
StoreAccess where userid and password is provided for each user. =
</B></P>
<P><B>Return userid if successful, else raise =
SQLException.</B></P>
<P>Code snippet is shown in the figure on the right.</P>
<P>Go back to your loginUser method in StoreAccessBean =
class.</P></TD>
<TD width=3D"63%">
<P><IMG height=3D571=20
=
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessDAOImplL=
oginUser.png"=20
width=3D595 align=3Dleft border=3D0 name=3DGraphic21><BR=20
clear=3Dleft><BR></P></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<P><B>In StoreAcessBean class under the loginUser method just add some =
debug=20
statements, as shown below in this code snippet.</B></P>
<P align=3Dleft><IMG height=3D139=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessBeanLogi=
nUser.png"=20
width=3D389 align=3Dleft border=3D0 name=3DGraphic19><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note : We don't have to call the loginUser method in=20
StoreAccessDAOImpl, as it being invoked by the loginUser method in=20
StoreAccessSession class which inherits StoreAccessBean class, that is =
the=20
StoreAccessSession class has overridden this method.</I></FONT></P>
<P align=3Dleft><FONT color=3D#800000><I>Code snippet from =
StoreAccessSession shown=20
below.</I></FONT></P>
<P><IMG height=3D132=20
src=3D"file:///F:/java_trainning/1206/tuscejb/3.files/StoreAccessSessionL=
oginUser.png"=20
width=3D510 align=3Dleft border=3D0 name=3DGraphic20><BR =
clear=3Dleft><BR><BR></P>
<H3>Add Callback Methods :</H3>
<P>Now, add callback methods to complete this bean as shown below.</P>
<OL>
<LI>
<P>setSessionContext.</P>
<LI>
<P>UnsetSessionContext.</P></LI></OL>
<P><FONT color=3D#800000><I>Note : These callback methods are invoked by =
the EJB=20
container.</I></FONT></P>
<P><B>Add a field to store sessionContext.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT face=3DSans><FONT =
size=3D3><B><FONT=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -