Building a CMP Based Payment Voucher Web Application By Using Struts and EJB In this tutorial, you will create a CMP based payment voucher web application by using Struts and EJB. You will learn how to create a Container Managed Entity Bean to store the voucher. A Session bean will be used to interact with the CMP. A Model-View-Controller architecture known as Struts will serve as the front end. The Web portion of this tutorial was adapted from the Building a CMP Based School Schedule Web Application tutorial by Jason Sholl. Prerequisites For The Tutorial 1. Web Tools Platform (WTP) project The WTP project can be downloaded from http://download.eclipse.org/webtools/downloads/ 2. JDK 1.4.2 Sun's JDK is available from http://java.sun.com/j2se/1.4.2/download.html 3. JBoss 3.2.3 JBoss is available from http://www.jboss.org/products/jbossas/downloads 4. XDoclet 1.2.3 XDoclet is available from http://xdoclet.sourceforge.net/xdoclet/install.html Installing The JBoss and XDoclet Runtimes In Eclipse In order to run the EJB and Web modules from Eclipse (which will allow you to test and debug the modules), Eclipse has to be told where to find JBoss, XDoclet and where to find the JDK. JBoss requires a JDK so it can compile java class and JSPs. 1. Select Window -> Preferences.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Building a CMP Based Payment Voucher Web Application By Using Struts and EJB
In this tutorial, you will create a CMP based payment voucher web application by using Struts and EJB. You will learn how to create a Container Managed Entity Bean to store the voucher. A Session bean will be used to interact with the CMP. A Model-View-Controller architecture known as Struts will serve as the front end. The Web portion of this tutorial was adapted from the Building a CMP Based School Schedule Web Application tutorial by Jason Sholl.
Prerequisites For The Tutorial1. Web Tools Platform (WTP) project
The WTP project can be downloaded from http://download.eclipse.org/webtools/downloads/
2. JDK 1.4.2Sun's JDK is available from http://java.sun.com/j2se/1.4.2/download.html
3. JBoss 3.2.3JBoss is available from http://www.jboss.org/products/jbossas/downloads
4. XDoclet 1.2.3XDoclet is available from http://xdoclet.sourceforge.net/xdoclet/install.html
Installing The JBoss and XDoclet Runtimes In EclipseIn order to run the EJB and Web modules from Eclipse (which will allow you to test and debug the modules), Eclipse has to be told where to find JBoss, XDoclet and where to find the JDK. JBoss requires a JDK so it can compile java class and JSPs.
2. Select Java -> Installed JREs from the menu on the left.
3. Select Add.... The Add JRE dialog will open. Name the runtime Sun JDK 1.4.2. Select Browser for the JRE home directory and choose the location where you installed the JDK 1.4.2. And then click OK.
4. The Sun JDK 1.4.2 now shows on the list of installed runtimes.
5. Select Server -> Installed Runtimes from the menu on the left.
6. Click on Add.... Select JBoss -> JBoss v3.2.3 and click Next. Click Browse and select the location where you installed JBoss and click Finish.
7. Check JBoss v3.2.3.8. Select XDoclet. Make sure the builder item is enabled. Click on the Browse... button and choose
the directory where you have installed XDoclet. Make sure that you choose the correct version. Click Apply.
9. Select XDoclet -> ejbdoclet. Check JBoss. Click Edit... button to make sure JBoss version is 3.2. Click Apply.
10. Select XDoclet -> webdoclet. Check JBoss. Click Edit... button to make sure JBoss version is 3.2. Click Apply.
11. Click OK to close the preferences dialog. JDK, XDoclet and JBoss are now configured in Eclipse.
Creating An EJB ProjectThe first step is to create an EJB project for the EJB module.
2. Click Next and fill in 'PaymentVoucherEJB' for the EJB project name, check 'Add project to an EAR' and fill in 'PaymentVoucherEAR' for the EAR Application name, click Next.
3. Click Next again and uncheck EJB client JAR support, and click Finish.
4. Once the wizard finishes, switch to the J2EE Perspective and see your newly created EJB Project.
Creating An EJB Session BeanNow you will create a Session bean in the PaymentVoucherEJB EJB project.
1. Select File -> New -> Other -> EJB -> XDoclet Enterprise JavaBean.
2. Click Next and check 'SessionBean'.
3. Click Next, fill in 'ejbs' for the package and 'PaymentVoucherSessionBean' for the class name.
4. Click finish to create your Session bean and see it in the Project Explorer. The annotations builder should have run and generated local and remote, and local and remote home interfaces.
It is important to remember when developing EJBs using Xdoclet to only edit the actual bean classes (e.g. ejbs.PaymentVoucherSessionBean). All deployment descriptor metadata, as well as the various EJB interfaces are generated using the XDoclet javadoc tags. If you open PaymentVoucherSessionBean, you can see some of these tags. Full documentation on how to use these tags can be found in the XDoclet docs.
Creating A CMP BeanAt the time of this writing, there is no tooling support Entity beans. Entity beans may be created, however, using JDT tools and XDoclet tags.
1. Right click on ejbs and select New -> Other -> Java -> Class, click Next.
2. Fill in the following information: Package: ejbs, Name: PaymentVoucherItemBean, Modifiers: public and abstract, Superclass: java.lang.Object, Interfaces: javax.ejb.EntityBean. Then click finish.
3. Now, open ejbs.PaymentVoucherItemBean.java in the Java editor. XDoclet tags need to be added in order for XDoclet to properly generate all the necessary code and ejb meta data. Start by adding the following javadoc to the bean class itself (right above the line: public abstract class PaymentVoucherItemBean). This code specifies that this bean is a CMP, should only have local interface, defines the interfaces names, and specifies that the primary key is type java.lang.Integer and is handled by field 'id'.
7. Next is to add a means by which to create PaymentVoucherItemBean. The create method will take the Name for the new PaymentVoucherItem and autogenerate a new key. The rudimentary key generation code is for example purposes only. After you paste the code below, do an organized imports (Control-Shift-o) to add an import for CreateException.
private static int PRIMKEY = (int) System.currentTimeMillis();
8. The last step is to add a few finder definitions. These should be added to the class level javadoc (right below @ejb.pk class definition added before).
* @ejb.finder signature="java.util.Collection findAll()"* query="select object(o) from PaymentVoucher o"* * @ejb.finder signature="java.util.Collection findByIc(String ic)"* query="select object(o) from PaymentVoucher o where o.ic = (?1)"** @ejb.finder signature="java.util.Collection findByName(String name)"* query="select object(o) from PaymentVoucher o where o.name = (?1)"
** @ejb.finder signature="java.util.Collection findByPrimaryKey(Integer id)"* query="select object(o) from PaymentVoucher o where o.id = ?1"
9. At this point if you save and build, you should be able to open PaymentVoucherItemLocal and PaymentVoucherItemLocalHome and see all method stubs were appropriately generated. Do not edit these files. Once you are done inspecting them, close them both.
Adding purpose to PaymentVoucherSessionBean1. If PaymentVoucherSessionBean is not open, open it in the JavaEditor. The first thing we need to
do is add a local reference from the PaymentVoucherSessionBean to the PaymentVoucherItemLocal. This requires the following XDoclet tag at the class level (put this right below the *@ejb.bean name=”PaymentVoucherSession” block). The second tag which starts with @jboss.ejb-local-ref is a JBoss specific XDoclet tag used to setup the JNDI name; other vendors also have their own specific tags where needed.
2. Since PaymentVoucherSessionBean will be used by the forthcoming web client instead of PaymentVoucherItemBean, it is necessary to add a few methods to access PaymentVoucherItems. The below code supplies a way for clients to create new PaymentVoucherItems, and find existing ones using the finders we specified previously. Note that PaymentVoucherItems beans are not returned directly, but, rather their data is wrapped up in a PaymentVoucherItemWrapper. The PaymentVoucherItemWrapper will be created in the next step. Delete the existing 'foo' method and add the following methods to PaymentVoucherSessionBean. You will have several compile errors which can be fixed using organized imports (Control-Shift-o); just be sure you select the correct types if there are multiple choices (javax.naming.Context, javax.rmi.PortableRemoteObject, java.util.List, java.util.Iterator).
} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();
}}
private List wrapPaymentVoucherItemsInList(Collection items) {Iterator iterator = items.iterator();List list = new ArrayList(items.size());while (iterator.hasNext()) {
// TODO Auto-generated catch blocke.printStackTrace();
}return null;
}
3. Next, PaymentVoucherItemWrapper needs to be created. Create a new Class called PaymentVoucherItemWrapper in the ejbs package. Be sure to add the java.io.Serializable interface. Then click Finish.
4. Next, open PaymentVoucherItemWrapper in the Java editor and add the following fields.
public Integer id;
public String type;
public String code;
public String description;
public float amount;
public String name;
public String ic;
public String paymentType;
5. Right click on PaymentVoucherItemWrapper in the source editor and select Source -> Generate Getters and Setters. Generate a getter and setter for each field.
Building a Struts ProjectThe first step is to create a Struts project. The easiest way is to import the struts-blank.war file.
1. Select File -> Import... -> WAR file and click Next.
2. Click Browse... and select the location where you downloaded the WAR file, fill in 'StrutsPaymentVoucher' for the Web project name and select 'JBoss v3.2.3' for target runtime, check 'Add module to an EAR application' and fill in 'PaymentVoucherEAR' for the EAR Application name. Click finish.
3. Expand the 'Dynamic Web Projects' node in the project explorer, and right click on StrutsPaymentVoucher and select 'Properties'.
4. Select the 'J2EE Module Dependencies' properties page, and then click the checkbox next to 'PaymentVoucherEJB.jar' to add a module dependency to the EJB module.
Creating struts-config.xml fileA Struts application has to have a Struts application configuration file. In this example and in most other simple Struts applications, the default name of the configuration file is struts-config.xml and it typically resides under application's ./WEB-INF directory. The configuration information is then read by Struts framework when the application gets started.
1. Open struts-config.xml in Text Editor if it is not open.
2. Add the following XML tags to define form bean definition and action mapping definition.
<!-- ========== Form Bean Definitions ================= --><form-beans>
Creating ActionForm ClassesNext, you will create a class which captures input form data that is entered by an end user. An ActionForm class is a JavaBean and each input form data field is mapped to a property of the ActionForm class. Consequently you write getter and setter methods for each of the properties.
1. Select File -> New -> Other -> Java -> Class and then click Next.
2. Fill in 'submit' for the package, and 'SubmitForm' for the class name and then click Finish.
3. Next, open SubmitForm in the Java editor and add the following code.
package submit;
import org.apache.struts.validator.ValidatorForm;
public final class SubmitForm extends ValidatorForm {
public void setSearchIC(String searchIC) {this.searchIC = searchIC;
}}
Creating Action ClassesNow you need to create two Action classes which the execute() method of the Action class get invoked by the Struts framework for each request.
1. Select File -> New -> Other -> Java -> Class and then click Next.
2. Fill in 'submit' for the package, and 'ListAction' for the class name and then click Finish.
3. Open ListAction in the Java editor and add the following code to provide business logic of your application.
Creating A JSPThis is the final piece of this example payment voucher application, and you are going to use Struts tags to provide a user interface. In this sample application, you need to create three JSP pages.
1. Select File -> New -> Other -> Web -> JSP and click next to create a new JSP page.
2. Select the WebContent directory under StrutsPaymentVoucher, and fill in index.jsp for the file name. Click finish.
3. Open index.jsp in the JSP editor and add the following code.
Creating web.xml fileIn this application, you need to define the ActionServlet into web.xml configuration file, which functions as a controller from the standpoint of the Model-View-Controller framework. Every web application must have a web.xml configuration file since Struts application is a web application. Before you adding the configuration, you need to run XDoclet to generate jboss-web.xml. It is because XDoclet will clear the content for ActionServlet definition and generate its own web.xml.
1. Right click on StrutsPaymentVoucher and then select Run XDoclet. After that, XDoclet will generate a file call jboss-web.xml as you can see at WEB-INF directory.
2. Next, open web.xml in the Text editor and add the following XML tags into web-app tag.
<!-- Standard Action Servlet Configuration (with debugging) --><servlet>
Running And Testing The Payment Voucher Web ApplicationYour payment voucher application is now complete. Now it is time to take it for a spin.
1. Select Window -> Show View -> Other... -> Server -> Servers and click OK.
2. Right click on the servers view and select New -> Server.
3. Select 'JBoss v3.2.3' under JBoss and click Finish.
4. Right click on the new server and select 'Add and Remove Projects...' which will bring up the dialog below.
5. Select PaymentVoucherEAR from the left panel and click 'Add >' to add it to the right panel as shown below. Click Finish.
6. Right click on the newly created server in the Servers view and select Start. Wait a few seconds to ensure the server started up correctly. The Console view should look something like this:
7. Now, back in the Project Explorer right click on 'index.jsp' and select 'Run As' -> 'Run on Server'.
8. This will bring up the below dialog. Select JBOSS 3.2.3 and check 'Set server as project default(do not ask again)' and click Finish.
9. An embedded web browser should open showing the following page.
10. You can experiment by adding more records into your payment voucher.
SummaryIn this tutorial you learned how to configure Eclipse to work with JBoss and create J2EE EJB and Struts projects that uses a CMP Bean, a Session Bean, an ActionForm class, an Action class, and a JSP to create a payment voucher J2EE Web application. This application, while simple, provides a good introduction to Java Web development and some of the Web development tools available in the Eclipse Web Tools Platform project.