Oracle9i JDeveloper - Hands on Labs (Frameworks)

Using A J2EE Framework To Create J2EE Applications

Introduction

This lesson demonstrates how you can create a set of business components based on tables in an Oracle9i database schema. You will do this using Oracle's Business Components For Java (BC4J) framework. After creating the basic framework using wizards, you will see how to test the application, add business logic, and then create a number of different types of user interface, all of which integrate seamlessly with the underlying framework objects, leaving them unchanged, thus allowing extensive reuse.

Exercises in this Lesson

  1. Create a Project Containing Business Components
  2. Add Business Logic
  3. Create a Java Client Application
  4. Create a Simple JSP Client
  5. Generate a Struts-based JSP Application for BC4J (optional)

Prerequisites

Please read the prerequisites and ensure that you have the proper environment to complete this lab.

1. Create a Project Containing Business Components

In this section you will create a new workspace and Business Components Project, using UML to model the entity objects in your project. The business components will be based on the OrderEntry schema and will serve as the business logic for your client applications.

1.1 Create a New Workspace and Project

  1. In the navigator, right-click the Workspaces node (at the top) and select New Workspace...
  2. In the New Workspace dialog (see figure 1 below):
    • Change the directory name to ...\jdev\mywork\BC4J.
      Note
      : You only need to change the last part of the path, the BC4J part. Do not change any part of the path prior to \mywork\.
    • Change the file name to BC4J.jws.
    • Make sure that Add a New Empty Project is selected.

    Figure 1: New Workspace Dialog

  3. Click OK.
  4. In the New Project dialog:
    • Change the directory name to ...\jdev\mywork\BC4J\OrderEntry.
    • Change the file name to OrderEntry.jpr.
    • Click OK.
    • Notice that an empty project (OrderEntry.jpr) has been added to the navigator. 

1.2 Add Business Components to Your Project

  1. In the navigator, right-click OrderEntry.jpr and select New Business Components Package... from the context menu.
  2. In the Welcome page of the Business Components Project Wizard, click Next.
  3. In Step 1 of the wizard (Package Name):
    • Change the package name to orderEntry,
    • make sure the first radio button is selected (Entity Objects Mapped to Database Schema Objects),
    • click Next.
  4. In Step 2 (Connection) select oeConnection as the connection and click Next.
  5. In Step 3 (Business Components), select the following tables in the Available list and click > to move them to the Selected list:
    • CUSTOMERS
    • ORDERS
    • ORDER_ITEMS
    • PRODUCT_INFORMATION
  6. Click Finish.
  7. Click the Save All button to save your work.

2. Add Business Logic

2.1 Format Data for Clients

Your business components project contains entity objects that map to database tables, view objects that map to clients, and an application module that packages the view objects into an application. In this exercise you will customize your business components to change the way the data is displayed in your client applications.

Format orders: remove attributes, add customer information and change the sort order

  1. In the navigator, double-click OrdersView.
  2. In the View Object editor, select the Entity Objects node.
  3. Select Customers in the Available list and click > to move it to the selected list. This allows you display some customer information with each order. Click Apply.
  4. Select the Attributes node.
  5. Select OrderStatus, OrderTotal, and SalesRepId in the Selected List and click < to move them to the Available list.
  6. Select CustLastName in the Available list and click > to move it to the selected list.

    Figure 2 below shows the resulting list:

    Figure 2: View Object Editor (OrdersView)

  7. Select the Query node.
  8. Click Edit (to the right of the Order By field) and move Customers.CUST_LAST_NAME into the Selected list, then click OK. This sorts the orders by the customer's last name.
  9. Click OK in the View Object editor.
  10. In the popup query dialog ("Would you like to test..."), click Yes, then click OK to accept the dialog telling you that the query is valid.

Format order items: add the product name, change the attribute order

  1. In the navigator, double-click OrderItemsView.
  2. In the View Object editor, select the Entity Objects node.
  3. Select ProductInformation in the Available list and click > to move it to the selected list. Click Apply.
  4. Select the Attributes node.
  5. Select Quantity in the selected list and click the up arrow button to move the attribute above UnitPrice.
  6. Select ProductName in the Available list and click > to move it to the selected list.

    Figure 3 shows the resulting list:

    Figure 3: View Object Editor (OrderItemsView)

  7. Click OK.

Simplify the application's data model

  1. In the navigator, double-click OrderEntryModule.
  2. In the Application Module editor, select the last item in the Data Model tree (on the right) and click < to remove it from the tree.
  3. Continue removing items until your data model contains only OrdersView1 and OrderItemsView2, as shown below in Figure 4:

    Figure 4: Data Model

  4. Click OK, then click the Save All button to save your work.

2.2 Compile and Test Your Business Components

The next step is to compile your project and test your business components, using the Business Component Browser (also called the tester).

  1. Click the Build button to compile your project
  2. Run the tester for the OrderEntryModule application module.
    • In the navigator, right-click OrderEntryModule and select Test... in the context menu as shown below in Figure 5.

      Figure 5: Testing an application module

  3. In the Connect dialog, click Connect.
  4. In the tester, double-click the OrderItemsOrderIdFkLink node to display a master-detail form for orders and order items (maximize the tester window to see more data).
  5. Use the navigation controls on the form to browse data, and notice that the tester shows the changes you have made to the view objects:
    • the customer name is shown with each order (grayed out because it cannot be changed)
    • orders are sorted by customer name
    • the product name is shown with each order item

  6. Select File > Exit to close the tester.

2.3 Format a Date Attribute

In this exercise you will format the date of an order so that it always displays as dd-MMM-yy (for example 01-Jan-02), on any client.

  1. In the navigator, select Orders
  2. In the structure pane, double-click OrderDate (see Figure 6)

    Figure 6: Structure pane

  3. In the Entity Attribute editor, select the Control Hints node.
  4. Select Simple Date as the format type.
  5. Enter a custom format dd-MMM-yy (alternatively you can select one of the values from the list).
  6. Click OK.
  7. (Optional): Use the same procedure to format the UnitPrice attribute in OrderItems. Select Currency as the format type.

2.4 Add Simple Business Logic

In this exercise you will add some simple business logic to your BC4J project: add a simple validation rule, and ensure that an attribute is refreshed after posting the record to the database. Business logic is usually added to entity objects.

  1. In the navigator, double-click OrderItems.
  2. In the Entity Object editor, select the Attribute Settings node.
  3. Select LineItemId from the drop-down list of attributes.
  4. Select DBSequence from the drop-down list of types. The database contains a trigger that sets the value of the underlying database column. Setting the attribute type to DBSequence provides a dummy value for this mandatory attribute, and refreshes the attribute with its new value after a new record is posted to the database. Click Apply
  5. Select the Validation node.
  6. Select Quantity and click New.
  7. In the Validation Rule dialog, select the CompareValidator rule.
  8. Select LessOrEqualTo as the operator.
  9. Ensure that Literal Value is selected in the Compare With list.
  10. Enter 5000 in the text area and click OK. Notice that your validation rule has been added to the declared validation rules.
  11. Click OK to dismiss the Entity Object editor. Your application now contains a validation rule to ensure that the quantity of an item cannot be more than 5000.

2.5 Write Java Code to Implement Business Logic

In this exercise you will add some more complex business logic by writing Java code. Your code will set the unit price depending on the quantity: orders of 100 or more get a 10% discount.

  1. In the navigator, expand the OrderItems node.
  2. Double-click OrderItemsImpl.java.
  3. In the .java file, find the method public void setQuantity(Number value).
  4. Delete the body of this method and replace it with the following code. This code will be executed whenever the Quantity attribute is set or changed:

    // Get the list price of the associated product. getProductInformation()
    // traverses an association to find the relevant ProductInformation object.

    Number price = getProductInformation().getListPrice();
    // For quantities of 100 or more, reduce the price by 10%.
    if (value.intValue() >= 100)
    {
    price = price.multiply(0.9);
    }
    // Call the appropriate setter method to set the unit price for this item.
    setUnitPrice(price);
    setAttributeInternal(QUANTITY, value);

2.6 Compile and Test Your Business Components

  1. Click the Build button to compile your project.
  2. Test the OrderEntryModule. In the navigator, right-click OrderEntryModule and select Test... in the context menu.
  3. In the Connect dialog, click Connect.
  4. In the tester, double-click the OrderItemsOrderIdFkLink node to display a master-detail form for orders and order items.
  5. Change an order item's quantity to a value greater than 5000 (for example, 99999), then click a different cell in the table. Notice that you get an exception popup telling you that you have entered an invalid value.
  6. Click OK to dismiss the dialog and change the quantity to 5000 or less, then move the cursor to a different cell. This time your change is accepted.
  7. In the navigation bar above the order items table, click the insert button to add a new order item. Notice that LineItemId has a dummy value.
  8. Enter a product id (some valid ids are:1726, 1729, 1733, 1734, 1737, 1738, 1739). Tab out of the field and notice that the product name is automatically inserted.
  9. Enter a quantity less than 100. Tab out of the field and notice that the unit price is automatically set.
  10. Change the quantity to 100. Move the cursor to a different cell, and notice that the unit price is reduced by 10%.
  11. Click commit to commit your changes to the database. Notice that LineItemId is refreshed with its new value.
  12. Select File > Exit to close the tester.

3. Create a Java Client Application

In this section you will create and customize a Java client for your business components, then run your client.

3.1 Create a Master-Detail Form

In this exercise you will use wizards to create a master-detail form.

  1. Create a new project in the BC4J workspace. In the navigator, right-click BC4J.jws and select New Empty Project... in the context menu.
  2. In the New Project Dialog:
    • Change the directory name to ...\jdev\mywork\BC4J\JClient
    • Change the file name to JClient.jpr
    • Notice that a new empty project JClient.jpr is added to your workspace.
  3. In the navigator, right-click JClient.jpr and select New... to invoke the New Gallery.
  4. In the New Gallery, select Client Tier | Swing/JClient for BC4J in the Categories list, then select Form in the Items list (see Figure 7 below) and click OK.


    Figure 7: New Gallery (JClient Form)

  5. In the JClient Form Wizard, Welcome page, click Next.
  6. In Step 1 (Form Types) select Master-Detail Tables as the form type and click Next.
  7. In Step 2 (Form Layout), select the following options:
    • Master template: Single columns (label left). Number of columns: 2.
    • Detail template: Single columns (label above). Number of columns: 2.
  8. In Step 3 (Data Model), click New. In the BC4J Client Data Model Definition Wizard, click Next through all the steps of the wizard, then click Finish to dismiss the BC4J Client Data Model Definition Wizard. Click Next.
  9. In Step 4 (Panel View) accept the default master (OrdersView1) and detail (OrderItemsView2) and click Next.
  10. In Step 5 (Attribute Selection) select CustomerId and CustomerId1 in the selected list. Click < to remove them from the list, then click Next.
  11. In Step 6 (Attribute Selection) move the following attributes from the selected list to the available list:
    • LineItemId
    • ProductId
    • ProductId1

    Click Next.

  12. In Step 7 (File Names) click Next then click Finish.

3.2 Compile and Run Your Java Client

  1. Click the Build button to compile your project.
  2. Run the client. In the navigator, right-click FormOrdersView1OrderItemsView2.java and select Run FormOrdersView1OrderItemsView2.java in the context menu.

    Figure 8 shows the running form; you may need to drag the edge of your form and make it wider.

    Figure 8: Running Java Client Application

  3. In your form, browse data and change some data. Notice that the business rules you created still apply in your new client:
    • The order date is formatted as dd-MMM-yy
    • Changing the quantity to a number greater than 5000 throws an exception.
    • Setting the quantity to a number greater than 100 gives a 10% discount on the unit price.
  4. Click commit to commit your changes to the database.
  5. Select File > Exit to close your form.

3.3 Customize Your Java Client Application

In this exercise you will make a simple change to your Java client application: since order mode has only two possible values, online and direct, you will replace the order mode field with a drop down list that the user can select from.

  1. Invoke the UI Editor for PanelOrdersView1.java. In the navigator, right-click PanelOrdersView1.java and select UI Editor in the context menu.
  2. In the UI Editor, select the OrderMode field (the small white box to the right of the OrderMode label) and press the Delete key.
  3. In the Component Palette, select the JComboBox icon.
  4. Move the cursor into the UI Editor, and position it to the right of the OrderMode label, where the OrderMode field used to be. Then click to add the combo box to the form. Your form should look like Figure 9.

    Figure 9: UI Editor.

  5. In the UI Editor, click inside the panel, then select the combo box you just added.
  6. In the property inspector (on the right of your screen), click the area to the right of the model property and select JClientEnumeratedBinding from the drop-down list.
  7. In the model dialog, select OrdersView1 in the first panel then select OrderMode in the second panel.
    This binds your new combo box to the OrderMode attribute so that the value selected is used to update the OrderMode attribute.
  8. Tab or click into the third panel (Provide a set of values) and type in the first value, "online" (without quotes). Press enter and type in the second value, "direct" (without quotes). This specifies the combo box's list of selections. Click OK.
  9. Run the application again. In the navigator, right-click FormOrdersView1OrderItemsView2.java and select Run FormOrdersView1OrderItemsView2.java in the context menu.

    Drag the right or left edge of the running form to widen the form.

    Notice that OrderMode is displayed as a drop down list. To change the value of this attribute you select a new value from the list.

  10. Click commit to commit your changes to the database.
  11. Click File > Exit to close the form.

4. Create a Simple JSP Client

In this section you will create a new project with a single JSP page that displays data from your business components.

4.1 Create a Project Containing a Simple JSP Page

In this exercise you will create a new project and add a simple JSP page.

  1. Create a new project in the BC4J workspace: in the navigator, right-click BC4J.jws and select New Empty Project... from the context menu.
  2. In the New Project Dialog:
    • Change the directory name to ...mywork\BC4J\Jsp
    • Change the file name to Jsp.jpr
    • Notice that a new empty project, Jsp.jpr, is added to your workspace.
  3. In the navigator, right-click Jsp.jpr and select New... in the context menu.
  4. In the New Gallery, select Web Tier | JavaServer Pages in the Categories list, then select JSP Page in the Items list (see Figure 10 below), and click OK.

    Figure 10: New Gallery (JSP)

  5. In the New JSP Dialog, accept the defaults and click OK.
  6. Notice that the code editor now shows the code for your new JSP, untitled1.jsp.
  7. Edit your JSP code, and change the title (between the <TITLE>...</TITLE> tags) from Hello World to Order Items.
  8. Delete all the code between the <BODY>...</BODY> tags, then press Enter a few times to insert some blank lines between these two tags.
  9. Position your cursor immediately below the opening <BODY> tag.

4.2 Add Data Binding to the JSP

You will now add code to display data from the Business Components you created earlier. This is done by inserting BC4J Data Tags into the JSP page using the Component Palette

  1. The Component Palette should be visible, probably on the right of your screen (Figure 11 shows the Component Palette). If the Component Palette is not visible, select View > Component Palette in the main menu.

    Figure 11: Component Palette

  2. In the Component Palette, select BC4J Connections in the drop-down list, then click Application Module.
  3. In the ApplicationModule dialog, make sure that orderEntry.OrderEntryModule is selected as the application module, then click Next.
  4. On the next page, click Finish.
  5. Notice that a <jbo:ApplicationModule...> tag has been added to your JSP. This identifies the BC4J application module that your page will use. The next step is to identify a DataSource (a BC4J view object) to the page.
  6. In the Component Palette, click DataSource.
  7. In the DataSource dialog, select OrdersView1 then click Next.
  8. On the next page, enter orders for the id attribute.
  9. Leave the remaining attributes blank and click Finish. Your JSP can now access data from OrdersView.
  10. Click the Save All button to save your work.

4.3 Displaying BC4J Data in a JSP

The next step is to add some tags to your JSP that will display order item data in an HTML table.

  1. In your JSP, type the following code, just below your datasource tag:

    <table border=1>

    </table>

    Note on autocompletion: JDeveloper automatically inserts the closing HTML tag for each opening tag. To add the tags above, type the opening tag, <table border=1>, then pause for a second and the closing tag, </table>, is automatically inserted. Press Enter twice to insert the blank line between the tags.

    Position the cursor between the <table>...</table> tags.

  2. In the Component Palette, select BC4J Data Access from the drop down list and click RowsetIterate.
  3. In the RowsetIterate dialog, select orders as the datasource and click Finish. This inserts the following code:
    <jbo:RowsetIterate datasource="orders" ></jbo:RowsetIterate>
  4. Place your cursor before the closing </jbo:RowsetIterate> and type table row tags, <tr> and </tr> (see Note on tag autocompletion, above). Then insert some blank lines between the tags, so that your code looks like this:
    <table border=1>
    <jbo:RowsetIterate datasource="orders"><tr>
    
    
    </tr></jbo:RowsetIterate>
    </table>
    
  5. Position your cursor in the blank space between the tags, and type table cell tags, <td> and </td> (see Note on tag autocompletion, above). Position your cursor in between these tags.
  6. In the Component Palette, click RenderValue.
  7. In the RenderValue dialog, select orders as the datasource, and OrderId as the dataitem. Click Finish. Your code now contains a RenderValue tag inside a table cell tag, like this:

    <td><jbo:RenderValue datasource="orders" dataitem="OrderId" ></jbo:RenderValue></td>

  8. Repeat the preceding three steps to insert another pair of table cell tags containing another RenderValue tag. This time select orders as the datasource, and OrderDate as the dataitem.
  9. When you have finished, the body of your JSP should look like this:

    <jbo:ApplicationModule id="OrderEntryModule" configname="orderEntry.OrderEntryModule.OrderEntryModuleLocal" releasemode="Stateful" />
    <jbo:DataSource id="orders" appid="OrderEntryModule" viewobject="OrdersView1" />
    <table border=1>
    <jbo:RowsetIterate datasource="orders" ><tr>
    <td><jbo:RenderValue datasource="orders" dataitem="OrderId" ></jbo:RenderValue></td>

    <td><jbo:RenderValue datasource="orders" dataitem="OrderDate" ></jbo:RenderValue></td>

    </tr></jbo:RowsetIterate>
    </table>

  10. Click the Save All button to save your work.
  11. To run the JSP:
    • select the untitled1.jsp node in the navigator
    • Context menu: Run untitled1.jsp.

    You will first see the embedded OC4J process initializing itself in the message window. When the browser appears, you should see a table containing order information. Notice that the order date is displayed with the custom format you specified for the OrderDate attribute.

2458 16-Aug-99
2397 19-Nov-99
2454 02-Oct-99
2354 13-Aug-00
2358 08-Jan-00

5. Generate a Struts-Based JSP Application For BC4J (optional)

Apache Struts is an open source controller framework for web applications that provides an Servlet that controls the event handling and page flow between JSP pages.  In this section you will use the JDeveloper to generate a BC4J JSP application which uses this Struts framework to control the application flow. The application that will be created is very similar to the one created by the previous exercise except that the links between pages are no longer hardcoded into the JSP files and you have the ability to add validation and pageflow logic into the Struts controller. The generated application includes pages to browse and edit data.

  1. Create a new project in the BC4J workspace.
  2. In the New Project Dialog:
    • Change the directory name to ...\jdev\mywork\BC4J\StrutsApp
    • Change the file name to StrutsApp.jpr
    • Notice that a new empty project, StrutsApp.jpr, is added to your workspace.
  3. In the navigator, right-click StrutsApp.jpr and select New... in the context menu.
  4. In the New Gallery, select Web Tier | Struts-Based JSP for Business Components in the Categories list, then select Complete Struts-Based JSP Application  in the Items list (see Figure 12 below), and click OK.

    Figure 12: Selecting a New Struts BC4J Application

  5. In the BC4J Struts JSP Application Wizard, Welcome page, click Next.
  6. In Step 1 (Data Definition) click New. In the BC4J Client Data Model Definition Wizard, click Next through all the steps of the wizard, then click Finish to dismiss the BC4J Client Data Model Definition Wizard. Click Next
  7. In Step 2 (View Object Forms):
    • Select OrdersView1 in the View Objects tree
    • Uncheck the Generate Page checkbox.
    • Select the OrderItemsView2 node and uncheck the Generate Page checkbox.
  8. In Step 3 (View Link Form), click Next (do not uncheck the Generate Page checkbox).
  9. In the final step, click Finish.
  10. Notice some of the files that have been added to your project:
    • A new set of JSPs
    • A Struts-Config.xml file. This file supplies the definition that is used by the Struts Servlet to control the page flow. The Struts-config file can be expanded in the navigated to examine each of the "Actions" which are handled by the servlet.  You can also view the Struts-Config.xml file by choosing Settings from the Context menu.
    • A file called ApplicationResources.properties which contains strings used by the Struts framework.
    • A set of Java files which define the Action used to populate the master detail page and the JavaBean that Struts uses to populate the edit form for the order items.
  11. Click the Save All button to save your work.
  12. Before running your application, you need to terminate any running instances of the embedded OC4J. In the main menu, select Run | Terminate | Embedded OC4J Server. If the Terminate menu item is disabled then the embedded OC4J is not running and you do not need to do anything.
  13. Run the Struts application:
    • In the navigator, right-click OrderItemsOrderIdFkLink1 (under the struts-config.xml node). 
    • Select Run OrderItemsOrderIdFkLink1 in the context menu.

      Figure 13: Running an Action from the Struts-Config

  14. When the browser loads your application, you will see a Master Detail page that allows you to navigate through the Orders and insert, update and delete Order Lines. Notice that the detail section shows three rows and has the title "OrderItemsView2 Browse Form"
  15. To change the number of rows displayed in the details section. Edit the OrderItemsOrderIdFkLink1Action.java class. Change the line vo.setRangeSize(3); to vo.setRangeSize(5);
  16. To change the title.  Open the ApplicationResources.properties file. change the value of the property browse.header to a suitable title e.g. browse.header=Order Lines
  17. Click the Build button to compile your project.
  18. Click the Save All button to save your work.
  19. Re-Run the Struts Application to see your changes.

    Note: You may need to terminate the running instance of embedded OC4J as described above.


Congratulations! You've completed the J2EE Frameworks Hands On lab.

If you have time, feel free to try out another lab!

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