Legal | Privacy
Developing a J2EE Application using TopLink, Struts, JSP and ADF Databinding

Developing a J2EE Application using TopLink, Struts, JSP and ADF Databinding

This lesson describes in detail how to bind TopLink business services to Struts and JSP pages using ADF Databinding.

Topics

This lesson will discuss the following:

Overview
Prerequisites
Step 1 - Create a Business Service Layer

Step 2 - Create a Data Control Layer

Step 3 - Create the Controller and View Layer for Department data.
Summary

1 hour

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 lesson steps you through the development of a Human Resources application using Toplink, Struts, and JSPs.

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'll create an application using the Model-View-Controller (MVC) architecture. You will create the model using Toplink to generate classes from database tables. You'll use the struts controller and will implement the view using JSP pages.

Prerequisites

Back to Topic List

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

1.

Install Oracle JDeveloper 10g.

 

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 a Business Service Layer

Back to Topic List

In this step you'll create a new application workspace to organize your application. You'll also create a new EJB diagram for your business tier objects. A diagram gives you a visual representation of your business objects, helping you to conceptualize the application. Once the EJB Diagram is created, you'll start building persistent business objects as entity beans.

Create a New Application Workspace
Reverse engineer tables as Java Classes
Generate TopLink Deployment Descriptor


Create a New Application Workspace

Back to List

1.

In the Application Navigator, right-click the Applications node and choose New Application Workspace. The Create Application Workspace dialog opens.

Change the Application Name to TopLinkHRApplication
In the Application Package Prefix field, type hr.
In the Application Templates field, choose No Template[All Technologies] from the dropdown list.

Click OK.

Move your mouse over this icon to see the image

A new project called Project1 is created.

 

2.

From the File menu, select Rename and name this project TopLinkModel.

Move your mouse over this icon to see the image

Click Save.

 

3.

The next step is to notify the IDE that TopLink is one of the technologies used by this project. This enables the IDE to provide smart choices as you develop your application.

Double-click on TopLinkModel in the navigator to access the Project Properties dialog.
Select the Technology Scopes node in the Project Properties dialog.
Select TopLink in Available Technologies and move it to Selected Technologies.
Select Java in Available Technologies and move it to Selected Technologies.

Move your mouse over this icon to see the image

 

4.

Modify the Default Package (under Common -> Input paths) to hr.model

Move your mouse over this icon to see the image

Click OK.

 

5.

From the File menu, choose Save All.

 

Reverse engineer tables as Java Classes

Back to List

1.

In the Application Navigator, right-click TopLinkModel (under the TopLinkHRApplication node) and select New...
The New Gallery opens.

In the New Gallery, under the Categories list, expand General, and select Diagrams. In the Items list, select Java Class Diagram.

Move your mouse over this icon to see the image

Click OK. The Create Java Class Diagram dialog opens.

 

2.

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

Move your mouse over this icon to see the image

Click OK.

 

3.

From the File menu, choose Save All.

 

4.

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

In the Navigator, click the Connections tab. This will bring the Connection Navigator to the front.
In the Connection Navigator, expand the nodes for Database, jdbc_tutorial_connection, hr, and Tables. Click on DEPARTMENTS and then Ctrl-click EMPLOYEES. Drag and drop both onto the blank Java Class diagram.

Move your mouse over this icon to see the image


The Create from Tables dialog opens.

 

5.

In the Create from Tables dialog, select Java Classes and click OK.

Move your mouse over this icon to see the image

The new Java class beans appear on the diagram.
Optionally, arrange the objects in the diagram by clicking and dragging the diagram elements so that they are easier to see. You can straighten the relationship lines by right-clicking a line and choosing Straighten Lines.

 

6.

In the Navigator, click on the Applications tab and you'll notice that two new nodes, Departments.java, and Employees.java, appear in the Application Navigator underneath the hr.model package.

Move your mouse over this icon to see the image

A new package HR is created which contains offline database tables.

 

7.

From the File menu, choose Save All.

 

 

Generate TopLink Deployment Descriptor

Back to List

1.

In the Navigator right-click TopLink Mappings (under the TopLink package) and select Generate toplink-deployment-descriptor.xml

Move your mouse over this icon to see the image

Click OK.

 

Step 2 - Create a Data Control Layer

Back to Topic List

The data control layer exposes the business services to clients. The data control is technology specific, so there are different data controls for TopLink, EJBs, JavaBeans, ADF business components, etc. Additionally, the data bindings are client agnostic. This means 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 is create the data control.

1.

Open the Data Control Palette by selecting View -> Data Control Palette.

Move your mouse over this icon to see the image

 

2.

Drag Departments.java from the Application Navigator and drop it onto the Data Control Palette.

Move your mouse over this icon to see the image

 

3.

From the File menu, choose Save All.

 

4.

Take a look at the Application Navigator and you'll notice a few new files. Departments.xml and Employees.xml (under the hr.model package) contain the metadata for the methods and accessors.

Move your mouse over this icon to see the image

 

 

Step 3 - Create the Controller and View Layer

Back to Topic List

In this step you'll create the view and controller part of this HR application. The controller separates the visual representation of web pages from their flow and actions. From the home page of this application, the user will be able to navigate to a JSP page which will show a list of all Departments. Selecting one of the departments will forward the user to a JSP containing a form for editing Department data. Finally, you'll create a JSP which displays a list of employees by department. This demonstrates master-detail navigation in a data-bound Struts JSP application.

Create a JSP to Browse Departments
Create a JSP to Edit Departments
Create a JSP to Browse Employees in a Department


Create a JSP to Browse Departments

Back to List

1.

Select the TopLinkHRApplication node, right-click and choose New Project.
In the New Gallery, under the Categories list, expand General, and select Projects. In the Items list, select Empty Project.

Move your mouse over this icon to see the image

Click OK.

 

2.

Name this project ViewController and click OK to create the project.

Move your mouse over this icon to see the image

 

3.

Now notify the IDE that Struts and JSP are the technologies used by this project.

Double-click on ViewController in the navigator to access the Project Properties dialog.
Select the Technology Scope node.
Select Struts in Available Technologies and move it to Selected Technologies.
Select JavaServer Pages (JSP) and Servlets in Available Technologies and move it to Selected Technologies.
Select Hyper Text Markup Language (HTML) in Available Technologies and move it to Selected Technologies.

Move your mouse over this icon to see the image

4.

Modify the Default Package (under Common -> Input paths) to view

Move your mouse over this icon to see the image

Click OK. This will setup your project for Struts development.

 

5.

In the Application Navigator, expand the ViewController project. Expand Web Content and WEB-INF.

Double-click on struts-config.xml. A blank Page Flow Diagram opens.

In the Component Palette select the Page component and add it to the Page Flow Diagram by clicking in the diagram. This creates a JSP page called /untitled1. Click within the name to change it to /home.jsp

Move your mouse over this icon to see the image

 

6.

In the Component Palette select the Data Page component and click on the diagram. Name this new Page /browseDept.

Move your mouse over this icon to see the image

 

7.

Double-click on /home.jsp to create the JSP page and open it in the visual JSP editor.
Optionally, add the text Home Page of My HR Application to the JSP. Add a style sheet and modify the JSP page as needed.

Move your mouse over this icon to see the image

 

8.

Open the Page Flow Diagram to draw the page flow between the shapes.
Choose the Page Link component on the Component Palette, and click on the /home.jsp action. Drag the link to the /browseDept Data Page and click again. (This will create a link in the home page which will invoke the /browseDept Data Page)

Move your mouse over this icon to see the image

 

9.

Double-click on /browseDept to create the JSP page. Accept the default name and click OK to open it in the visual JSP editor.
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

 

10.

In the Data Control Palette, select return from the readAllDepartments()node.
In the Drag and Drop As dropdown list, select Read-Only Table.

Move your mouse over this icon to see the image

 

11.

Drag and drop DepartmentsCollection | return from the Data Control Palette onto the browseDept page.

Move your mouse over this icon to see the image

 

12.

The ApplicationResources.properties file contains messages that we will be using in the application. Change the properties file to have a user friendly link in the home.jsp JSP page.
Double-click ApplicationResources.properties under the view package in the Application Navigator.

Move your mouse over this icon to see the image

 

13.

Change the value of link.browseDept to Click here to browse Departments.

Move your mouse over this icon to see the image

 

14.

In the Page Flow Diagram, select the /home.jsp icon. Right-click and select Run.

Move your mouse over this icon to see the image

Click the link to view department data.

 

 

Create a JSP to Edit Departments

Back to List

1.

In order to update the department we need to keep track of the current row of the department selected by the user in the browseDept.jsp page. We will update the browseDept.jsp so that a link is added to select the department.

In the Page Flow Diagram, double-click on /browseDept to open it. Select the Source tab to view the code.
Place the cursor in front of <c:out value="${Row.departmentId}"></c:out>

Move your mouse over this icon to see the image

 

2.

In the Data Control Palette, select return in the readAllDepartments() node.
In the Drag and Drop As dropdown list, select Select Row Link.

Move your mouse over this icon to see the image

 

3.

Drag the return node from the Data Control Palette onto the browseDept.jsp page, just before the cursor, and change the href to point to setCurrentDept.do

Move your mouse over this icon to see the image

Note: We will create setCurrentDept Data Page in the next step.

 

4.

Open the Page Flow Diagram.
In the Component Palette select the Data Action component and click on the diagram. Name this new Data Action /setCurrentDept.

Move your mouse over this icon to see the image

 

5.

Choose the Link component in the Component Palette, click on the /browseDept.jsp page, drag the link to the /setCurrentDept Data Action, and click again.

Move your mouse over this icon to see the image

 

6.

In the Data Control Palette, expand the Operations node in DepartmentsCollection and select setCurrentRowWithKey(String).

Move your mouse over this icon to see the image

 

7.

Drag setCurrentRowWithKey(String) from the Data Control Palette to /setCurrentDept in the Page Flow Diagram.

 

8.

Next create a JSP to display an input form to edit the department information.

Select the DataPage component from the component palette and add it to the Page Flow Diagram. Change the name to /editDept.

Move your mouse over this icon to see the image

 

9.

Select the Forward component on the Component Palette and click the /setCurrentDept data action. Drag the link to the /editDept and click again.

Move your mouse over this icon to see the image

 

10.

Double-click /editDept and click OK to to create the corresponding JSP page and open it in the visual editor.
Optionally, add 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

 

11.

In the Data Control Palette, select the return node from readAllDepartments().
In the Drag and Drop As dropdown list, select Input Form.

Move your mouse over this icon to see the image

 

12.

Drag return from the Data Control Palette onto the editDept.jsp page.

Move your mouse over this icon to see the image

 

13. In the Page Flow Diagram, right-click /home.jsp and select Run.
Click on the link Click Here to Browse Departments in the home page.
You will see the list of departments. Notice the * next to the department number. This identifies the current row.

Move your mouse over this icon to see the image

 

14.

Click on the Select for a department number.
You will see the edit page which has the information related to department number you have selected.

Move your mouse over this icon to see the image

Note: Don't update the data yet, we will add the update functionality in the next step.

 

15.

Open editDept.jsp in the visual editor.
In the Data Control Palette, select Commit from the Operations node.
In the Drag and Drop As dropdown list, select Button.

Move your mouse over this icon to see the image

 

16.

Drag Commit from the Data Control Palette onto the editDept.jsp page.

Move your mouse over this icon to see the image

 

17.

In the Data Control Palette, select Rollback from the Operations node.
In the Drag and Drop As dropdown list, select Button.

Move your mouse over this icon to see the image

 

18.

Drag Rollback from the Data Control Palette onto the editDept.jsp page.

Move your mouse over this icon to see the image

 

19.

Now add a link to the browse page from the edit page. Open the Page Flow Diagram. Choose the Page Link component from the Component Palette, and drag it from /editDept to /browseDept.

Move your mouse over this icon to see the image

 

20.

From the File menu, choose Save All.

 

21.

In the Page Flow Diagram, right-click /home.jsp and select Run. Click Click Here to Browse Departments in the home page.
You will see the list of departments.

Move your mouse over this icon to see the image

 

22.

Click on Select for a department number.
You will see the edit page which has the information related to the department you have selected.

Move your mouse over this icon to see the image

 

23.

Update department information and click Submit.
This enables the Commit and Rollback buttons. Click Commit.


Move your mouse over this icon to see the image

Click on the link Click Here to Browse Departments, which will display the updated department information.

 

 

Create a JSP to Browse Employees in a Department

Back to List

1.

Open the Page Flow Diagram.
In the Component Palette, select the Data Page component and click on the diagram. Name this new Data Page /browseEmp.

Move your mouse over this icon to see the image

 

2.

Choose the Page Link component in the Component Palette, click on the /home.jsp action, drag the link to /browseEmp and click again. (This will create a link in the home page which will invoke /browseEmp Data Action).

Move your mouse over this icon to see the image

 

3. Double-click on /browseEmp to create the JSP page.
Optionally, add the text List of Employees by 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

 

4.

In the Data Control Palette, select the return node of DepartmentsCollection.
In the Drag and Drop As dropdown list, select Read-Only Form.

Move your mouse over this icon to see the image

 

5. Drag and drop DepartmentsCollection from the Data Control Palette onto the browseEmp JSP page.

Move your mouse over this icon to see the image

 

6.

In the Data Control Palette, select the return node within DepartmentsCollection.
In the Drag and Drop As dropdown list, select Navigation Buttons.

Move your mouse over this icon to see the image

 

7.

Drag return to the browseEmp JSP page.
(Make sure that the buttons are dropped within the boundaries of the Form).

Move your mouse over this icon to see the image

 

8.

In the Data Control Palette, select the employeesCollection node.
In the Drag and Drop As dropdown list, select Table.

Move your mouse over this icon to see the image

 

9.

Drag employeesCollection onto the browseEmp JSP page, beneath the Departments form.

Move your mouse over this icon to see the image

 

10.

Change the Struts ApplicationResource.properties file to have a user-friendly link in the home.jsp page.
Select ApplicationResources.properties under the view package in the Application Navigator.
Change the value of link.browseEmp to
Click Here to See the List of Employees by Department.

Move your mouse over this icon to see the image

 

11.

In the Page Flow Diagram, right-click /home.jsp and select Run.
Click on the link you just created.
In the employees list page, click on the navigation buttons to see the employees in different departments.

Move your mouse over this icon to see the image

 

 

In this lesson, you learned how to generate Java classes from database tables using Toplink, create a Page Flow using the Struts Controller, and how to create JSP pages which are bound to data using ADF databinding.

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