Login | Register   
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.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

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.*" %> <html:html> <head> <title>Retrieve Stock Information</title> </head> <body> <h2>Choose a Stock</h2> <html:errors/> <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> </html:select><br> <html:submit/> <br> <br> <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> </table> </html:form> </body> </html:html>

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-beans> <form-bean name="stockDynaValidateForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="ticker" type="java.lang.String"/> <form-property name="stocks" type="lum.stock.Stocks"/> </form-bean> </form-beans>

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-mappings> <action path="/stockselect" type="lum.stock.action.StockAction" name="stockDynaValidateForm" validate="true" scope="request"> <forward name="success" path="/pages/stockquery.jsp"/> <forward name="failure" path="/pages/stockquery.jsp"/> </action> <action XXX other actions XXX> </action> </action-mappings>

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

Comment and Contribute






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



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