Legal | Privacy
Developing a Web Application Using the EJB Technology Scope

Developing a Web Application Using the EJB Technology Scope

This tutorial teaches you how to develop a web application using JSP pages, Struts, and Enterprise JavaBeans.

Topics

This lesson discusses the following:

Overview
Prerequisites
Step 1 - Create the Business Service Layer

Step 2 - Create the Data Control Layer

Step 3 - Create a JSP to Browse Departments
Step 4 - Create a JSP to Insert New Departments

Step 5 - Create a JSP to Edit Departments

Summary

This tutorial should take about an hour to complete.

Move your mouse over this icon to show all screenshots. You can also move your mouse over each individual icon to see only the screenshot associated with it.


Overview

This document describes in detail the steps required to create and bind EJB business services to Struts and JSP pages using ADF Databinding. You will implement the design patterns (J2EE Blueprints) pioneered by Sun Microsystems, a set of best practices for developing J2EE applications. Specifically, you build your application using the MVC architecture (a broad design pattern that divides an application into the Model, View, and Controller parts). You also implement the Data Transfer Object patterns.

Back to Topic List

You have been given the task of creating a Human Resources application that can be deployed to multiple platforms, and can use any type of client. To accomplish this task you create an application using Enterprise JavaBeans.

Prerequisites

Back to Topic List

In order for this lesson to work successfully, you should have performed the following:

1.

Install Oracle JDeveloper10g.

 

2.

Install the sample schema and create a connection to the HR or HR8 schema to use in this lesson. See Installing the Sample Schemas and Establishing a Database Connection.

 

 

Step 1 - Create the Business Service Layer

In this step you create a new EJB diagram for your business tier objects. A diagram gives you a visual representation of your business objects, helping you conceptualize and organize your application. Once the EJB Diagram is created, you start building your persistent business objects. In this case, your persistent business objects are entity beans. You can create entity beans by dragging them from a database table and dropping them onto an EJB diagram. Follow these steps to create the EJB diagram:

Create a New Application Workspace
Reverse engineer tables as CMP Entity beans
Create a Session Bean

Create a Session Facade

Create Data Transfer Objects (DTOs)
Add Business Methods


Create a New Application Workspace

Back to List

1.

In the Application Navigator, right-click the Applications node and choose New Application Workspace.

Move your mouse over this icon to see the image

The Create Application Workspace dialog opens.

 

2.

Change the Application Name to HRApplication.
In the Application Templates field, choose Web Application [JSP, Struts, EJB] from the dropdown list.

Move your mouse over this icon to see the image


Click OK to create the Application Workspace. The new application is represented in the Application Navigator by two new projects, Model and ViewController.

 

3.

From the File menu, choose Save All.

 

 

Reverse engineer tables as CMP Entity beans

Back to List

1.

In the Application Navigator, right-click Model (under the HRApplication node) and choose New.

Move your mouse over this icon to see the image


The New Gallery opens.

 

2.

In the New Gallery, under the Categories list, expand Business Tier, and select Enterprise JavaBeans. In the Items list, select EJB Diagram.

Move your mouse over this icon to see the image

Click OK. The Create EJB dialog opens.

 

3.

Accept the default package name, but change the name of the diagram to EJB HR Diagram.

Move your mouse over this icon to see the image

Click OK.

 

4. From the File menu, choose Save All.
5.

In the Navigator, click the Connections tab.
In the Connection Navigator, expand the nodes for Database, jdbc_tutorial_connection, HR, and Tables.

Move your mouse over this icon to see the image

If you have not established this connection to the HR schema, please see prerequisites section.

 

6.

Select DEPARTMENTS and drag it onto the blank EJB diagram.

Move your mouse over this icon to see the image


The Create from Tables dialog opens

 

7.

In the Create from Tables dialog, select EJB 2.0 Entity Beans and click OK.

Move your mouse over this icon to see the image

The new entity bean appears on the diagram.

 

8.

In the Navigator, click on the Applications tab and you'll notice that a new node, Departments, appears in the Application Navigator in the model package.

Move your mouse over this icon to see the image

From the File menu, choose Save All.

 

 

Create a Session Bean

Back to List

1.

In the Component Palette, click Session Bean and then click inside the EJB diagram.

Move your mouse over this icon to see the image

The Create Enterprise JavaBean Wizard opens.

 

2.

Review the information on the welcome page and click Next.
On the Name and Options page, change the EJB Name to hrApp.

Move your mouse over this icon to see the image

Click Next.

 

3.

On the Class Definitions page, accept the default values and click Next.
For this sample we use local interfaces as the client JSP pages are running in the same OC4J instance. On the EJB Home and Component Interfaces page, clear the checkbox for Include Remote Interfaces and select the box for Include Local Interfaces.

Move your mouse over this icon to see the image

Click Finish.

 

 

Create a Session Facade

Back to List

Session Facade is the most widely used of all EJB design patterns. A session facade allows you to properly partition the business logic in your system to help minimize dependencies between the client and server, while forcing use cases to execute in one network call and in one transaction. In this step you add a local reference from the session bean to the entity beans.

1.

In the Component Palette, choose EJB Local Reference.

Move your mouse over this icon to see the image

 

2.

In the diagram, click on the session bean (hrApp), drag to the entity bean (Departments) and click again.

Move your mouse over this icon to see the image

The local reference is displayed as a line between hrApp and Departments.

 

3. From the File menu, choose Save All.

 

Create Data Transfer Objects (DTOs)

Back to List

The Data Transfer Object (DTO) design pattern provides better maintainability by separating use cases and entity beans and by reusing entity beans across different applications. It also increases performance because the attributes from multiple entity beans can be passed to the client within one call. Creating a DTO for an entity bean is very easy in JDeveloper.

1.

Select the Departments bean in the EJB diagram.
Right-click and select Generate | Data Transfer Object.

Move your mouse over this icon to see the image

 

2.

If the generated DTO is off the screen on the diagram, select Thumbnail from the View menu to navigate through your EJB diagram.

Move your mouse over this icon to see the image

 

3. From the File menu, choose Save All.

 

Add Business Methods

Back to List

In this step you add business methods to your session bean.

1.

On the EJB diagram, click in the first empty cell of the hrApp session bean and add a new attribute named showDepartments : Collection.

Move your mouse over this icon to see the image

 

2.

Click in the second empty cell and add a new method named updateDepartment(DepartmentsLocalDTO dept) : void

Move your mouse over this icon to see the image

 

3.

Click in the next cell and add a new method named addDepartment (DepartmentsLocalDTO dept) : void

Move your mouse over this icon to see the image

 

4.

On the diagram, right click hrApp and choose Go to Source | hrAppBean.java Bean Class.
The Code Editor opens.

Move your mouse over this icon to see the image

 

5.

In the Code Editor, scroll down to the getShowDepartments() method.
Modify this method so that it returns a collection of Department DTOs by copying and pasting the following code:

public Collection getShowDepartments()
{
 try
 {
 ArrayList al = new ArrayList();
 Collection col = this.getDepartmentsLocalHome().findAll();
 DepartmentsLocal dept;
 Iterator it = col.iterator();
 while(it.hasNext())
  {
  DepartmentsLocalDTO deptsDTO = new DepartmentsLocalDTO((DepartmentsLocal)it.next());
  al.add(deptsDTO);
  }
 return al;
 }
 catch (FinderException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
 catch (NamingException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
}

 

6.

Locate the updateDepartment() method and modify the method by copying and pasting the following code:

public void updateDepartment(DepartmentsLocalDTO dept)
{
 try
 {
 DepartmentsLocal deptlocal = this.getDepartmentsLocalHome().findByPrimaryKey(dept.getDepartmentId());
 deptlocal.setDepartmentName(dept.getDepartmentName());
 deptlocal.setLocationId(dept.getLocationId());
 deptlocal.setManagerId(dept.getManagerId());
 }
 catch (FinderException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
 catch (NamingException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
}

Note: The method only updates Department Name and Location ID. Additional logic can be added to update other fields.

 

7.

Modify the addDepartment() method by copying and pasting the following code:

public void addDepartment(DepartmentsLocalDTO dept)
{
 try
 {
 this.getDepartmentsLocalHome().create(dept.getDepartmentId(),
                                       dept.getDepartmentName());
 }
 catch (NamingException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
 catch (CreateException e)
 {
 System.out.println(e.toString());
 throw new javax.ejb.EJBException(e);
 }
}

8.

Import the following classes:

import java.util.ArrayList;
import java.util.Iterator;
import javax.ejb.FinderException;

import javax.ejb.CreateException;

 

9.

In the Application Navigator, right-click hrApp and choose Make to compile.

Move your mouse over this icon to see the image

 

 

Step 2 - Create the Data Control Layer

Back to Topic List

The data control layer exposes the business services to clients. The data control is technology specific, so that there are different data controls for EJBs, JavaBeans, ADF business components, etc. The data bindings are client agnostic, meaning that no matter what kind of technology the business services were developed with, a client can interact with any of them seamlessly. The ADF framework creates the data bindings for you, so the only thing you need to do in this step is to create the data control.

1.

From the View menu, choose Data Control Palette.

Move your mouse over this icon to see the image

The Data Control Palette opens.

 

2.

Drag hrApp from the Application Navigator to the Data Control Palette.

Move your mouse over this icon to see the image

 

3.

Notice the following files in the Application Navigator:

hrAppLocalDataControl.java is the DataControl that exposes the methods and accessors in the business service layer.
hrAppLocalDataControlBeanInfo.java is the bean info class, which hides the system methods appearing in the Data Control Palette.
hrAppLocalDataControl.xml contains the metadata for the methods and accessors.

 

4.

In the Application Navigator, click hrAppLocalDataControl.xml.
In the Structure pane, select showDepartments.

Move your mouse over this icon to see the image

 

5.

From the View menu, choose Property Inspector.
Select the Bean Class field and click the ellipsis (...).

Move your mouse over this icon to see the image

The Bean Class dialog opens.

 

6.

In the Bean Class dialog, click Browse.
In the Class Browser, select model.DepartmentsLocalDTO and click OK.

Move your mouse over this icon to see the image

Click OK to close the Bean Class dialog.
DepartmentsLocalDTO.xml is generated. This file contains the metadata for the collection.

 

7.

From the File menu, choose Save All.

 

Step 3 - Create a JSP to Browse Departments

Back to Topic List

In this step you start working on the controller part of your MVC application. The controller separates the visual representation of web pages from their flow and actions. You create a page flow diagram, which visually organizes the flow and actions of your web pages, and create a JSP for browsing Departments using the showDepartments method of the hrApp session bean.

1.

In the Application Navigator, expand the ViewController project, the Web Content folder, and the WEB-INF folder. Double-click struts-config.xml to see a blank page flow diagram.

Move your mouse over this icon to see the image

 

2.

In the Component Palette, click the Data Page element and then click on the diagram to create it.

Move your mouse over this icon to see the image

A new data page appears on the diagram.

 

3.

Change the name to /browseDepartments.

Move your mouse over this icon to see the image

Double-click /browseDepartments to create the JSP. Accept the default JSP name.

 

4.

Optionally, add the text List of Departments to the JSP. Add a style sheet and modify the JSP page as needed.

Move your mouse over this icon to see the image

 

5.

On the Component Palette, click the Data Controls tab. On the Data Control Palette, click showDepartments. In the Drag and Drop As dropdown list, select Read-Only Form.

Move your mouse over this icon to see the image

 

6.

Drag showDepartments from the Data Control Palette and drop it onto browseDepartments.jsp in the Editor.

Move your mouse over this icon to see the image


A new read-only form is created on the page. Right now the read-only form is functional, but it would only show the first record in the Departments table.

 

7.

In the Data Control Palette, expand the nodes for showDepartments and Operations, and click First.
In the Drag and Drop As dropdown list, select Button.

Move your mouse over this icon to see the image

 

8.

Click inside the border of the read-only form you created (but not inside the table cells) to drop the button.

Move your mouse over this icon to see the image

 

9.

In a similar manner, drop Previous, Next, and Last buttons from the Data Control palette to browseDepartments.jsp.

Move your mouse over this icon to see the image

Drag the buttons around if you want to arrange them differently.

 

10.

From the File menu, choose Save All.

 

11.

On the Page Flow Diagram, right-click /browseDepartments and select Run. Navigate through the Department rows using the navigation buttons.

Move your mouse over this icon to see the image

 

 

Step 4 - Create a JSP to Insert New Departments

Back to Topic List

In this step you continue building the application to insert new departments using the add() method of the hrApp session bean.

 

1.

First create a page flow for inserting new department data. In the Component Palette, click the Page element and then click on the diagram to create it. Change the name to /createDepartments.jsp.

Move your mouse over this icon to see the image

 

2.

In the Component Palette, click the Page Link element. Click on /browseDepartments and click again on /createDepartments.jsp to add a link from the browse page to the insert page.

Move your mouse over this icon to see the image

 

3.

Now create a data action to create the new row. In the Component Palette, click the Data Action element and click on the diagram to create it. Change the name to /createDepartmentsDA.

Move your mouse over this icon to see the image

 

4.

In the Data Control Palette, expand hrAppLocalDataControl and Operations. Select addDepartment(DepartmentsLocalDTO) and verify that Method is selected in the Drag and Drop As dropdown list.

Move your mouse over this icon to see the image

Drag and drop the addDepartment() method to the /createDepartmentsDA data action.

 

5.

Next, override the method in the createDepartmentsDA data action to take the parameters from Request object and create a DTO.

Right click /createDepartmentsDA and choose Go To Code.
In the Create Struts Data Action dialog, change the package name to view.actions.

Move your mouse over this icon to see the image

This creates a separate package where all struts actions can reside.
Click OK to create CreateDepartmentsDAAction.java.

 

6.

From the Tools menu, choose Override Methods...

Move your mouse over this icon to see the image

 

7.

In the Override Methods dialog, select the initializeMethodParameters() checkbox.

Move your mouse over this icon to see the image

Click OK.

 

8.

Update the initializeMethodParameters() method in CreateDepartmentsAction.java with the following code:

protected void initializeMethodParameters(DataActionContext actionContext, JUCtrlActionBinding actionBinding)
{
 // Construct a new DepartmentLocalDTO to be the single parameter for the
 // createDepartments() method
 DepartmentsLocalDTO dept = new DepartmentsLocalDTO();

 // Populate the DepartmentsLocalDTO with data from the form
 String data = actionContext.getHttpServletRequest().getParameter( "departmentId" );
 if ( data != null )
  {
  dept.setDepartmentId( new Long( data ) );
  }
 data = actionContext.getHttpServletRequest().getParameter( "departmentName" );
 if ( data != null )
  {
  dept.setDepartmentName( data );
  }
 data = actionContext.getHttpServletRequest().getParameter( "locationId" );
 if ( data != null )
  {
  dept.setLocationId( new Long( data ) );
  }
 data = actionContext.getHttpServletRequest().getParameter( "managerId" );
 if ( data != null )
  {
  dept.setManagerId( new Long( data ) );
  }

 // Add this DepartmentsLocalDTO to the parameter list for this data action
 ArrayList params = new ArrayList();
 params.add( dept );
 actionBinding.setParams( params );
}

 

9.

Import the following classes:

import model.DepartmentsLocalDTO;
import java.util.ArrayList;

 

10.

In the application navigator, double-click the ViewController project.
In the Project Properties dialog, select the Libraries category in the Development node.
Select J2EE in the Available Libraries and use the arrow buttons to move it to Selected Libraries

Move your mouse over this icon to see the image

Click OK. Right-click CreateDepartmentsDAAction.java in the code editor and select Make.

 

11.

Return to the Page Flow Diagram. Choose the Forward component in the Component Palette, click on /createDepartmentsDA, drag the forward component to the /browseDepartments data page, and click again.

Move your mouse over this icon to see the image

 

12.

From the page flow diagram, double-click /createDepartments.jsp to create the page and open it in the visual JSP editor.
Optionally, add the text Insert a New Department to the JSP. Add a style sheet and modify the JSP page as needed.

Move your mouse over this icon to see the image

 

13.

Use the HTML category of the Component Palette to create an html form containing a table with 4 rows and 2 columns.

Move your mouse over this icon to see the image

 

14.

Add labels and text fields to the table for Department Id, Department Name, Location Id, and Manager Id.

Move your mouse over this icon to see the image

 

15.

Select the text field for the department id and use the Property Inspector to specify the name of the text field according to the name used in the session bean: departmentId

Move your mouse over this icon to see the image

 

16. Set the name property of the remaining text fields in the same manner, using the names departmentName, locationId, and managerId, respectively.
17.

In the Component Palette, select the Submit button and drag below the table to create it.

Move your mouse over this icon to see the image

 

18.

Click inside the blank area of the html form to select it. In the Property Inspector, change the action property to /createDepartmentsDA.do

Move your mouse over this icon to see the image

 

19.

Now change the link from browseDepartments.jsp to createDepartments.jsp to user-friendly text. In the Application Navigator, expand the view project under Application Sources. Double click the ApplicationResources.properties file.

Move your mouse over this icon to see the image

 

20.

Change the value for link.createDepartments to Insert a New Department.

Move your mouse over this icon to see the image

 

21.

From the File menu, choose Save All.

 

22.

On the Page Flow Diagram, right-click browseDepartments.jsp and select Run. Click the Insert a New Department link to add a new Department.

Click Submit to create the new department and return to the browse page.

 

 

Step 5 - Create a JSP to Edit Departments

Back to Topic List

In this step you continue building the application to edit department information using the update() method of the hrApp session bean.

1.

Now create a Data Action to select the current row for editing or deleting. In the Component Palette, click Data Action.

Move your mouse over this icon to see the image

 

2.

Click on the page flow diagram to create the Data Action and rename it to /updateDepartmentDA.

Move your mouse over this icon to see the image

 

3.

In the Component Palette, select the Data Page component and click on the page flow diagram to create a new data page. Name the page /editDepartment.

Move your mouse over this icon to see the image

 

4.

Choose the Forward component in the Component Palette, click on /updateDepartment, drag the forward component to the /browseDepartments Data Page, and click again.

Move your mouse over this icon to see the image

 

5.

In the Data Control Palette, expand Operations. Select updateDepartment(DepartmentsLocalDTO) and verify that Method is selected in the Drag and Drop As dropdown list.

Move your mouse over this icon to see the image

Drag and drop the updateDepartment method to the /updateDepartmentDA data action.

 

6. The updateDepartment() method accepts a DTO as its parameter. Thus, override the method in the updateDeptartmentsDA Data Action to take the parameters from Request object and create a DTO.

Double-click /updateDepartmentDA.
In the Create Struts Data Action dialog, accept the default name and package

Move your mouse over this icon to see the image

This creates a separate package where all struts actions can reside.
Click OK to create UpdateDepartmentsDAAction.java.

 

7.

From the Tools menu, choose Override Methods...

Move your mouse over this icon to see the image

 

8.

In the Override Methods dialog, select the initializeMethodParameters() checkbox.

Move your mouse over this icon to see the image

Click OK.

 

9.

Update the initializeMethodParameters() method in UpdateDepartmentDAAction.java with the following code:

protected void initializeMethodParameters(DataActionContext actionContext, JUCtrlActionBinding actionBinding)
{
 // TODO: Override this oracle.adf.controller.struts.actions.DataAction method
 // super.initializeMethodParameters(actionContext, actionBinding);

 DepartmentsLocalDTO dept = new DepartmentsLocalDTO();

 String data = actionContext.getHttpServletRequest().getParameter( "departmentId" );
 if ( data != null )
  {
  dept.setDepartmentId( new Long( data ) );
  }
 data = actionContext.getHttpServletRequest().getParameter( "departmentName" );
 if ( data != null )
  {
  dept.setDepartmentName( data );
  }
 data = actionContext.getHttpServletRequest().getParameter( "locationId" );
 if ( data != null )
  {
  dept.setLocationId( new Long( data ) );
  }
 data = actionContext.getHttpServletRequest().getParameter( "managerId" );
 if ( data != null )
  {
  dept.setManagerId( new Long( data ) );
  }
 ArrayList deptparams = new ArrayList();
 deptparams.add(dept);
 actionBinding.setParams(deptparams);
}

 

10.

Import the following classes:

import model.DepartmentsLocalDTO;
import java.util.ArrayList;

 

11.

In the Application Navigator, expand the View Controller, Application Sources, and view nodes.
Select browseDepartmentsUIModel.xml and in the Structure Pane, select updateDepartment.

Move your mouse over this icon to see the image

 

12.

In the Property Inspector, set the Requires Update Model property to True.

Move your mouse over this icon to see the image

 

13. From the file menu, chose Save All.
14.

From the page flow diagram, double-click /editDepartment to create the page and open it in the visual JSP editor.
Optionally, add the text Edit Department Information to the JSP. Add a style sheet and modify the JSP page as needed.

Move your mouse over this icon to see the image

 

15.

On the Component Palette, click the Data Controls tab. On the Data Control Palette, click showDepartments. In the Drag and Drop As dropdown list, select Input Form.

Move your mouse over this icon to see the image

 

16.

Drag showDepartments from the Data Control Palette and drop it onto editDepartment.jsp in the Editor.

Move your mouse over this icon to see the image

 

17.

Select the text field for the departmentId.
In the Property Inspector, set the readOnly property to true.

Move your mouse over this icon to see the image

 

18.

In the structure pane, select html:form.

Move your mouse over this icon to see the image

 

19.

In the Property Inspector, change the action property to /updateDepartmentDA.do.

Move your mouse over this icon to see the image

When the Submit button is pressed in editDepartment.jsp, the updateDepartmentDA data action is called.

 

20.

Open /browseDepartments.jsp in the JSP visual editor. Select the Source tab to view the code.
Place the cursor in front of <c:out value="${bindings.departmentId}"/>&nbsp;

Move your mouse over this icon to see the image

 

21.

In the Data Control Palette, expand showDepartments and Operations and select setCurrentRowWithKey(String)
In the Drag and Drop As dropdown list, select Find Row Link.

Move your mouse over this icon to see the image

 

22.

Drag the Find Row Link from the Data Control Palette onto the browseDepartments.jsp page and change the href to point to editDepartment.do

Move your mouse over this icon to see the image

 

23.

From the File menu, choose Save All.

 

24. In the Page Flow Diagram, right-click /browseDepartments.jsp and select Run.
25. Click on the Select link next to the Department id.
Edit the Department information and click Submit.

 

In this tutorial you learned how to use JDeveloper to develop a web-based application that implements the MVC design pattern, using JSP pages, Struts, Enterprise JavaBeans, and the data binding features of the Oracle Application Development Framework (Oracle ADF).

Installing the Sample Schemas and Establishing a Database Connection

 

Move your mouse over this icon to hide all screenshots

 

 

 

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy