Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Practical Guide for Integrating EJB and Struts : Page 3

EJB and the Struts framework each have received plenty of coverage individually, but what about leveraging EJB and Struts together? This tutorial lays out a complete step-by-step guide to integrating these two technologies.


advertisement
Create the EJB
As mentioned previously, this EJB will be a stateless session bean. For the sake of simplicity, it has just one method, which is passed the argument from the stockquery.jsp. It then returns the requested stock data, which is stored inside the bean itself.

Create the class under your source directory with a package name of lum.stock.ejb.bean (see Listing 2).

Notice the javadoc in this code. It is essential, as you will use XDoclet to generate several files (including your deployment descriptors) and both home and remote interfaces for the StockBean. (I discuss the steps for generating your files via XDoclet later.)



A closer look into the javadoc also reveals that I used a local interface for the bean.

WebDoclet JavaDoc Class
Your next step is to create a class composed of only javadoc, specifically for use by XDoclet. Creating the class ensures that your web.xml and jboss-web.xml files are refreshed/recreated every time you run XDoclet. Create the following class under your source directory with a package name of lum.webdoclet:

package lum.webdoclet; /** * @web.ejb-ref name="ejb/Stock" * type="Session" * home="lum.stock.ejb.interfaces.StockHome" * remote="lum.stock.ejb.interfaces.Stock" * description="Reference to the Stock EJB" * @jboss.ejb-ref-jndi ref-name="ejb/Stock" * jndi-name="lum/stock/ejb/bean/Stock" */ public class WebDoclet { }

Create the Façade Class
If you follow MVC, your EJB should be called from your action classes when you integrate it into a Struts framework. Looking at this application, you can see that indeed EJB creation and its method calls are performed in the StockAction class. Though on closer inspection you'll notice that a class called StockFacade actually wraps the EJB. This technique is known as façade and is based on the GOF façade pattern.

Take a closer look at the GOF façade and J2EE session façade patterns. You should ostensibly use the GOF façade pattern when you create a simple interface to provide access into a complex part of an application. The session façade is fundamentally the GOF design pattern applied to EJB. Whereas this application uses a simple java class as its façade and the stateless session bean as its complex component, the session façade pattern uses a stateless session bean as its façade, which in turn usually accesses multiple entity beans. Table 1 highlights the differences between this application's façade and the session façade.

Table 1. Differences Between Sample Façade and Session Façade
This Application Session Façade Pattern
Façade StockFacade class Stateless Session Bean
EJB StockBean Entity Bean(s)

The following is the StockFacade class:

package lum.stock.facade; import lum.stock.Stocks; import lum.stock.ejb.interfaces.*; import javax.naming.*; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.rmi.RemoteException; public class StockFacade { private StockHome home; public StockFacade() {} public Stocks getStockBean(String p_ticker) throws RemoteException { lookupStockBean(); Stock bean = createStockBean(); return bean.retrieveStockInfo(p_ticker); } private void lookupStockBean() { try { Context context = new InitialContext(); Object ref = context.lookup("java:/comp/env/ejb/Stock"); home = (StockHome)PortableRemoteObject.narrow(ref, StockHome.class); } catch (Exception e) { System.out.println("Lookup of java:/comp/env/ejb/Stock failed"); } } private Stock createStockBean() { Stock bean = null; try { bean = home.create(); } catch (Exception e) { System.out.println("Exception thrown on creation of bean from home interface"); } return bean; } }

You have now finished the coding portion; the rest of this article will focus on how to generate the necessary files using XDoclet, as well as packaging and deploying your application.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap