testcontent
This tutorial teaches you how to develop a web application using JSP pages, Struts, and Enterprise JavaBeans.
This lesson discusses the following:
|
Overview | |
|
Prerequisites | |
|
Step 1 - Create the Business Service Layer | |
|
||
|
Step 3 - Create a JSP to Browse Departments | |
|
Step 4 - Create a JSP to Insert New Departments | |
|
||
|
Summary | |
Time to complete
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.
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 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.
|
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 Data Transfer Objects (DTOs) | |
|
Add Business Methods | |
| 1. |
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.
|
| 3. |
From the File menu, choose Save All.
|
| 1. |
In the Application Navigator, right-click Model (under the HRApplication node) and choose New.
|
| 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.
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.
|
| 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. From the File menu, choose Save All.
|
| 1. |
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.
Click Next .
|
| 3. |
On the Class Definitions page, accept the default values and click
Next.
Click Finish.
|
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.
|
| 3. | From the File menu, choose Save All. |
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.
|
| 2. |
If the generated DTO is off the screen on the diagram, select Thumbnail from the View menu to navigate through your EJB diagram.
|
| 3. | From the File menu, choose Save All. |
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.
|
| 5. |
In the Code Editor, scroll down to the
getShowDepartments()
method.
public Collection getShowDepartments()
|
| 6. |
Locate the updateDepartment() method and modify the method by copying and pasting the following code: public void updateDepartment(DepartmentsLocalDTO dept)
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)
|
| 8. |
Import the following classes: import java.util.ArrayList;
|
| 9. |
In the Application Navigator, right-click hrApp and choose Make to compile.
|
0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } //-->
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.
|
| 4. |
In the Application Navigator, click
hrAppLocalDataControl.xml.
|
| 5. |
From the View menu, choose
Property Inspector.
The Bean Class dialog opens.
|
| 6. |
In the Bean Class dialog, click
Browse.
Click
OK to close the Bean Class dialog.
|
| 7. |
From the File menu, choose Save All.
|
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.
|
| 7. |
In the Data Control Palette, expand the nodes for showDepartments and Operations, and click
First.
|
| 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.
This creates a separate package where all struts actions can reside.
|
| 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)
// Populate the DepartmentsLocalDTO with data from the form
// Add this DepartmentsLocalDTO to the parameter list for this data action
|
| 9. |
Import the following classes: import model.DepartmentsLocalDTO;
|
| 10. |
In the application navigator, double-click the
ViewController project.
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.
|
| 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.
This creates a separate package where all struts actions can reside.
|
| 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: protected void initializeMethodParameters(DataActionContext actionContext, JUCtrlActionBinding actionBinding)
DepartmentsLocalDTO dept = new DepartmentsLocalDTO(); String data = actionContext.getHttpServletRequest().getParameter( "departmentId" );
|
| 10. |
Import the following classes: import model.DepartmentsLocalDTO;
|
| 11. |
In the Application Navigator, expand the
View Controller,
Application Sources, and
view nodes.
|
| 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.
|
| 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.
|
| 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.
|
| 20. |
Open
/browseDepartments.jsp in the JSP visual editor. Select the
Source tab to view the code.
|
| 21. |
In the Data Control Palette, expand
showDepartments and
Operations and select
setCurrentRowWithKey(String)
|
| 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
|
| 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).
Related topics
Installing the Sample Schemas and Establishing a Database Connection
Move your mouse over this icon to hide all screenshots