This tutorial should take about an hour to complete.
Viewing Screenshots
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.
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.
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:
In the Application Navigator, right-click the
Applications node and choose New Application Workspace.
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.
Click OK to create the Application Workspace.The new application is represented in the Application
Navigator by two new projects, Model and ViewController.
In the Application Navigator, right-click Model
(under the HRApplication node) and choose New.
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.
Click OK. The Create EJB dialog opens.
3.
Accept the default package name, but change the
name of the diagram to EJB HR Diagram.
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.
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.
The Create from Tables dialog opens
7.
In the Create from Tables dialog, select EJB
2.0 Entity Beans and click OK.
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.
In the Component Palette, click Session Bean
and then click inside the EJB diagram.
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.
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.
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.
2.
In the diagram, click on the session bean (hrApp), drag to the
entity bean (Departments) and click again.
The local reference is displayed as a line between hrApp and Departments.
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.
2.
If the generated DTO is off the screen on the
diagram, select Thumbnail from the View menu to navigate through
your EJB diagram.
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.
2.
Click in the second empty cell and add a new method named updateDepartment(DepartmentsLocalDTO
dept) : void
3.
Click in the next cell and add a new method named addDepartment (DepartmentsLocalDTO
dept) : void
4.
On the diagram, right click hrApp and choose Go
to Source | hrAppBean.java Bean Class.
The Code Editor opens.
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:
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.
The Data Control Palette opens.
2.
Drag hrApp from the Application Navigator to the Data Control
Palette.
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.
5.
From the View menu, choose Property Inspector.
Select the Bean Class field and click the ellipsis (...).
The Bean Class dialog opens.
6.
In the Bean Class dialog, click Browse.
In the Class Browser, select model.DepartmentsLocalDTO and click
OK.
Click OK to close the Bean Class dialog.
DepartmentsLocalDTO.xml
is generated. This file contains the metadata for the collection.
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.
2.
In the Component Palette, click the Data Page element and then
click on the diagram to create it.
A new data page appears on the diagram.
3.
Change the name to /browseDepartments.
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.
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.
6.
Drag showDepartments from the Data Control Palette and drop it
onto browseDepartments.jsp
in the Editor.
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.
8.
Click inside the border of the read-only form you created (but not inside
the table cells) to drop the button.
9.
In a similar manner, drop Previous, Next, and Last buttons
from the Data Control palette to browseDepartments.jsp.
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.
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.
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.
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.
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.
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.
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...
7.
In the Override Methods dialog, select the initializeMethodParameters()
checkbox.
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 );
}
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
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.
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.
13.
Use the HTML category of the Component Palette to create an html form
containing a table with 4 rows and 2 columns.
14.
Add labels and text fields to the table for Department Id, Department
Name, Location Id, and Manager Id.
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
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.
18.
Click inside the blank area of the html form to select it. In the Property
Inspector, change the action property to /createDepartmentsDA.do
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.
20.
Change the value for link.createDepartments to Insert a New
Department.
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.
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.
2.
Click on the page flow diagram to create the Data Action and rename it
to /updateDepartmentDA.
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.
4.
Choose the Forward component in the Component Palette, click on
/updateDepartment, drag the forward component to the /browseDepartments
Data Page, and click again.
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.
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
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...
8.
In the Override Methods dialog, select the initializeMethodParameters()
checkbox.
Click OK.
9.
Update the initializeMethodParameters()
method in UpdateDepartmentDAAction.java with the following code:
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);
}
In the Application Navigator, expand the View Controller, Application
Sources, and view nodes.
Select browseDepartmentsUIModel.xml and in the Structure Pane,
select updateDepartment.
12.
In the Property Inspector, set the Requires Update Model property
to True.
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.
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.
16.
Drag showDepartments from the Data Control Palette and drop it
onto editDepartment.jsp
in the Editor.
17.
Select the text field for the departmentId.
In the Property Inspector, set the readOnly property to true.
18.
In the structure pane, select html:form.
19.
In the Property Inspector, change the action property to /updateDepartmentDA.do.
When the Submit button is pressed in editDepartment.jsp,
the updateDepartmentDA data action is called.