RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


A Practical Guide for Integrating EJB and Struts : Page 2

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.

The Application
Your application will do a simple bean retrieval, based on a user requesting stock information. It involves a simple JSP, which requests that the user select a stock and submit. Your action class will then create a façade object and retrieve the information requested from your simple session bean (EJB).

You will first build the Struts components of the application, and then integrate EJB into the framework.

Create the JSP and Interface for Static Values
Create a stockquery.jsp under the /pages directory in your project. The following is the JSP I created:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page import="lum.staticvalues.*" %> 
<title>Retrieve Stock Information</title>
<h2>Choose a Stock</h2>

<html:form action="stockselect.do">
  <html:select property="ticker">
    <html:option value="<%=StaticValues.SUNW%>">Sun Microsystems</html:option>
    <html:option value="<%=StaticValues.IBM%>">International Business Machines</html:option>
    <html:option value="<%=StaticValues.CSCO%>">Cisco Systems</html:option>

  <table column="2">
	<tr><td>Ticker Symbol</td> <td><html:text property="stocks.tickerSymbol"/></td></tr>
	<tr><td>Change</td> <td><html:text property="stocks.change"/></td></tr>
	<tr><td>Days Range</td> <td><html:text property="stocks.daysRange"/></td></tr>
	<tr><td>Fifty Two Week Range</td><td><html:text property="stocks.fiftyTwoWeekRange"/></td></tr>
	<tr><td>Volume</td><td><html:text property="stocks.volume"/></td></tr>
	<tr><td>Avg Volume (3 months)</td><td><html:text property="stocks.avgVol"/></td></tr>
	<tr><td>Market Cap</td><td><html:text property="stocks.marketCap"/></td></tr>

Notice in the JSP code that you import an interface called StaticValues to hold the ticker symbols. In the event that a stock's ticker symbol changes—which certainly happens—you naturally want to make the change in only one place. Create the interface with the following code, with a package name of lum.staticvalues:

package lum.staticvalues;

public interface StaticValues {
	public static final String SUNW = "SUNW";
	public static final String IBM = "IBM";
	public static final String CSCO = "CSCO";

Create the Bean Class
Create a simple bean, Stocks.java, under your source directory (WEB-INF/src/java) with a package name of lum.stock (see Listing 1).

Create the Action Class
Now, create an action class called StockAction under your source directory, and keep it in the package stock lum.stock.action:

package lum.stock.action;

import org.apache.struts.action.*;
import org.apache.struts.validator.*;
import javax.servlet.http.*;
import lum.stock.Stocks;
import lum.stock.facade.StockFacade;
import java.rmi.RemoteException;

public class StockAction extends Action {

	public ActionForward execute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		DynaValidatorForm dvf = (DynaValidatorForm) form;
		String ticker = (String)dvf.get("ticker");  
//<form-property name="ticker" type="java.lang.String"/>
if (ticker!= null || !ticker.equals("")) { StockFacade sf = new StockFacade(); Stocks stks = new Stocks(); try { stks = sf.getStockBean(ticker); } catch(RemoteException re){ System.out.println("Caught Remote Exception from Stockbean"); } dvf.set("stocks", stks);
//sets via <form-property name="stocks" type="lum.stock.Stock"/>
} return mapping.findForward("success"); } }

Edit struts-config.xml
You need to insert only two entries into struts-config.xml to get the application running. Between the beginning and end <form-beans> tags, insert the following:

    	<form-bean name="stockDynaValidateForm"
		      <form-property name="ticker" type="java.lang.String"/>
		      <form-property name="stocks" type="lum.stock.Stocks"/>

Notice that I use DynaValidatorForm. I don't include any validation rules for the page. I leave that to you, should you decide to expand upon this application.

Your second entry is the action. Insert the following between the beginning and end <action-mapping> tags:

<action path="/stockselect"
	  <forward name="success" path="/pages/stockquery.jsp"/> 
	  <forward name="failure" path="/pages/stockquery.jsp"/> 

	<action XXX other actions XXX>

At this point, you've finished developing your Struts framework. Now it's time to develop your EJB components and incorporate them into Struts.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date