Using Jakarta Struts with Oracle9i JDeveloper

Introduction

Welcome to the Hands On Lab exercises for using Jakarta Struts with Oracle9i JDeveloper. These hands on exercises will familiarize you with how to use Struts in JDeveloper. The Struts exercises contain both simple exercises as well as more advanced exercises which integrate both Struts and BC4J.

This Hands On exercise requires the usage of Oracle9i JDeveloper version 9.0.3 and access to a database.

What is Struts?

Jakarta Struts is an open source project supported by the Apache software foundation. Struts
provides a framework using standard J2EE components such as JSPs, servlets, and JavaBeans for
building Model 2-a variant of Model View Control application design paradigm-applications.

Struts was written to solve problems encountered by many enterprise application development
projects in that it provides solutions to common problems encountered when developing enterprise
Web applications such as page flow, internationalization, maintainability, and deployment flexibility.

Struts is now fully integrated into Oracle9i JDeveloper allowing developers to take advantage of all of it's strengths from within a powerful Java Integrated Development Environment. (IDE).

The hands-on exercises are contained in the following Labs:

Lab 1 Building a Simple Struts Application

Exercise 1: Getting Started: Struts enabling your project
Exercise 2: Building your View Components
Exercise 3: Building Struts Form Beans and Actions
Exercise 4: Wiring it all together


Lab 2 Using Struts and BC4J


Exercise 1: Building a simple BC4J Project
Exercise 2: Building a simple BC4J Struts page

Exercise 3: Generating a BC4J Struts JSP application


Lab 1 Building a Simple Struts Application

In this Lab exercise we will be building a simple, easy to understand Struts application. This application consists of a simple login page along with some accompanying Struts based validation logic. Here is a diagram of the Application we will build:

Figure1: A Simple Struts Application

Notice that the App contains 3 JSPs, a login page, and a success and failure page. As the user submits the login form, he/she will then be directed to either the success page or failure page depending on whether the information supplied in the login form passed validation.

Exercise 1: Getting Started: Struts Enabling Your Project

In order to start using Struts in an application in JDeveloper, you must run the "Struts Starter Application" wizard to Struts enable your project.

Getting Started 

  1. In JDeveloper, create a new Workspace or use an existing one. (The name of Workspace is not important.)
  2. In your Workspace create a new project called SimpleStruts.jpr
    File | New... | General | Empty Project
  3. Name the directory, "SimpleStruts" and the project file, " SimpleStruts.jpr".
  4. Once the project has been created, run the Struts Starter Application Wizard.
    Select File | New
    Choose Web-Tier in the Categories, and select Struts
    Select Starter Application from the Items list
    Click OK.


  5. Click Yes to run the Struts starter wizard.
  6. The wizard will now Struts enable your project by doing the following:
    • Adds the Struts runtime libraries to your project's properties.
    • Adds (or Modifies) your web.xml file to use the Struts Controller (ActionServlet) servlet. It also defines a *.do servlet mapping.
    • Adds a struts-config.xml file to your project. This is the overall configuration file for a Struts application.
    • Adds an ApplicationResources.properties file to your project. This file contains message strings for the overall application.
  7. Save the project using File | Save All.

Exercise 2: Building Your View Components

We will now create the View Components login.jsp, success.jsp and failure.jsp. The JSP page login.jsp will contain an html form which is generated from the Struts JSP tags.

  1. In the same project as before SimpleStruts.jpr, build a login JSP page.
    Select File | New
    Choose Web-Tier in the Categories, and select JavaServer Pages (JSP)
    Select JSP Page from the Items
    Click OK.
  2. Name the page, login.jsp.

  3. You should now have a new login JSP file added to your project and invoked in the editor.

  4. Now let's build a Struts enabled form to accept a userid and password. To use the Struts JSP tags click on the Component Palette dropdown list and select "Struts HTML".
    Note: If the Component Palette is not visible, it can be invoked from the menu: View | Component Palette.

  5. Change the JSP's default header to:

    <h2>Login Page</h2>

  6. On the next line after the header, add a Struts error tag. This tag calls the form validator and lists the form validation errors.
    From the component palette select "errors" and click OK to add the tag without any attributes.

    <html:errors />

  7. Now let's add the Struts Form tag. Place the cursor In the line after the errors tag. Select "form" from the component palette. This time we'll set the "action" attribute to be "/login". The "/login" is a reference to our Struts "login" Action which we will be creating in a later step.

    <html:form action="/login">


  8. Using the component palette, add two fields: userid and password to the body of the form. (Note: Make sure these tags are added before the closing </html:form> tag.)

    <html:text property="userid" />
    <br>
    <html:password property="password" />

    When run, these tags will render HTML form input fields for the userid and password. Later on we will create a Struts Form bean which has the corresponding fields, userid and password.

  9. Now let's add field prompts to the form. For this we will use the Struts Bean tag "message". On the component palette, select "Struts Bean" to select this library onto the palette. Click on "message" in the palette and set the "key" attribute to "userid.prompt".

  10. Add another prompt for the password field in a similar fashion. This time set the key to "password.prompt".

  11. The final Struts tag you will need is a Struts submit tag. This will render a form submission button.
    This tag is located on the Struts HTML palette page.

    <html:submit />


  12. You can use an HTML table to align your field elements. Here's an example of what your final Form code should look like:

    <html:form action="/login">
    <table border="0">
    <tr>
    <td><bean:message key="userid.prompt" /> :</td>
    <td><html:text property="userid" /></td>
    </tr>
    <tr>
    <td><bean:message key="password.prompt" /> :</td>
    <td><html:password property="password" /></td>
    </tr>
    </table>
    <html:submit />
    </html:form>


  13. Select File | Save All.
    (Note: Successfully compiling the application is not yet possible. We still have to create the elements referred to by this form.)
  14. Now let's build the other pages success and failure jsp pages. Create these pages by running the JSP Page wizard as before and naming the files success.jsp and failure.jsp.
  15. In the source of the success and failure jsp pages modify the header as follows:

    <h2>
    Successful Login!
    </h2>

    (success.jsp)

    <h2>
    Login Failure!
    </h2>

    (failure.jsp)

  16. Select File | Save All.

    We will now build a Struts Form bean which will represent the fields in the form field.

 

Exercise 3: Building Struts Form Beans and Actions

This Exercise shows how to create the Struts Form bean and Action for the Login form.

Building a Struts Form bean
  1. In the same project as before, SimpleStruts.jpr, build a Struts form bean.
    Select File | New
    Choose Web-Tier in the Categories, and select Struts
    Select ActionForm from the Items
    Click OK.

  2. Enter the following fields:



    Note: The package name is whatever your default package name is for the project.

  3. After clicking OK, you should have a new java file LoginForm.java added to your project. Now let's customize the Form Bean.

    The first thing we will do is add the fields userid and password to the Login form bean. This can be done by editing the bean in the class editor.
  4. Invoke the Class editor on the Login Form bean:



  5. Once the class appears, click on the "Fields" tab at the bottom of the class editor window.
  6. Add a new field for "userid" by clicking on the "+ Add" button in the class editor.
  7. Set the field name to "userid", accept the remaining defaults and click "OK".
  8. Repeat the steps to create a field for "password". Your fields should look like:


  9. Notice the getter and setter methods have been added to the bean as well.



  10. Now we will modify the validate() method in LoginForm.java to check if the fields submitted weren't empty.
    In the code editor, modify the validate() method as follows:

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
    ActionErrors errors = new ActionErrors();
    if ((userid == null) || (userid.length() < 1))
    errors.add("userid", new ActionError("error.userid.required"));
    if ((password == null) || (password.length() < 1))
    errors.add("password", new ActionError("error.password.required"));
    return errors;
    }

  11. Select File | Save All and close the class and code editor windows.

Building a Struts Action
  1. In the same project as before, SimpleStruts.jpr, build a Struts Action.
    Select File | New
    Choose Web-Tier in the Categories, and select Struts
    Select Action from the Items
    Click OK.

  2. Enter the following fields:


  3. Edit the execute method of the new Java file LoginAction.java as follows:

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
    System.out.println("validating user");
    LoginForm loginForm = (LoginForm) form;
    String userid = loginForm.getUserid();
    String password = loginForm.getPassword();


    if ( userid.equals("scott") && password.equals("tiger") )
    {
    return mapping.findForward("success");
    }
    else return mapping.findForward("failure");
    }

    Notice that this code allows the login form to only accept the userid: scott/tiger.



  4. Select File | Save All.
  5. You should now be able to compile your project. Select Project | Rebuild SimpleStruts.jpr Alt + F9

 

Exercise 4: Wiring it All Together

Now that you've created the Struts View Components (JSPs) and the Struts middle tier components (Actions and formbeans), you will need to "wire" these components together. This is done by editing the Struts-Config.xml file. The Struts-Config.xml file is a master configuration file for all of the Struts application components. JDeveloper provides a visual editor for enhanced editing of the Struts-config.xml file.

Defining the Struts Action 

  1. Invoke the Struts-config editor by right-clicking on the Struts-config.xml file and selecting "Edit Struts-Config"


  2. Once the Struts-Config editor appears, click on the "Action Mappings" node on the left. You will see your "/login" action.
  3. Now click on the "/login" action in the Action Mappings window.
  4. Click on the "Form Bean" tab in the Properties window below.
  5. Select your "LoginForm" from the dropdown list for "Name:"
  6. Make sure the Validate() checkbox is checked.
  7. Enter "login.jsp" in the "input" field.
  8. Click "Ok" to dismiss the Struts Config editor and apply the changes.
    (Note: Due to bug: 2737866, you must make sure that you handle each Struts-Config operation separately. Such as whenever you change a setting on each node on the left such as an Action Mapping or a Global Forward, you must click "OK" each time to accept the changes before configuring something else.)

    See below image for reference:



  9. Now we will add the Global Forwards "success" and "failure" that we referred to in our Action.
  10. Re-invoke the Struts-Config editor. This time we will add some Global Forwards.
  11. In the Struts-Config editor click on "Global Forwards" node on the left tree.
  12. Click on the "Add" button on the right.
  13. Name the new Forward, "success" and associate it to the path "/success.jsp".

  14. Now repeat the same step for "failure" forward and specify "/failure.jsp" as its path.
  15. Click "OK" to dismiss the Struts-Config Editor.
  16. Select File | Save All.

The final touches: Editing the ApplicationResources.properties File

The ApplicationResources.properties file contains our messages that we will be using in the application. This includes both our message text on the login form as well as error messages when there is a validation error.

  1. Edit the ApplicationResources.properties.
  2. Add the following "keys" to the properties file:

    userid.prompt=<b>Userid</b>
    password.prompt=<b>Password</b>
    error.userid.required=<li>Username is required</li>
    error.password.required=<li>Password is required</li>

    Notice that these are the messages referred to from the application..


  3. Select File | Save All.
  4. Close the editor. You will now be able to run the application.
  5. Run the application by right-clicking on the login.jsp file and selecting Run login.jsp.
  6. As the application appears, try submitting an empty form. (Just click "submit")
    Notice the error message appear at the top of the form as it redraws.





  7. Now enter incorrect login information: bogus/bogus...
  8. You should see the Failure page appear!




  9. Now enter the correct login: scott/tiger and submit to see the "success" page.

 

Lab 2 Using Struts and BC4J

In this lab we will show how to use Struts with BC4J. After building a simple BC4J project we will then build a simple BC4J Struts app manually. The final exercise shows how to generate a complete Struts enabled BC4J JSP application.

Exercise 1: Building a Simple BC4J Project

For the BC4J exercises we will refer to a simple emp/dept bc4j project.

  1. In the same workspace create a new Business Components project using the Business Components Project Wizard:
    File | New... | General | Projects | Project Containing Business Components...



  2. Name the directory, bc4j and the project file, bc4j.jpr.
  3. As the project is created, the Business Components wizards is automatically invoked.
  4. Accept the defaults. Note: You must have a connection created for a database containing the scott/tiger schema.
  5. On the final step of the BC4J Wizard, select Emp and Dept to create ViewObject/Entity Objects.


  6. Click Finish to generate your Business Components.
  7. Now Save and compile your project bc4j.jpr.

Exercise 2: Building a Simple BC4J Struts Page

Create a StrutsBC4J project

  1. Create a new project called StrutsBC4J.jpr
    File | New... | General | Empty Project
  2. Name the directory, "StrutsBC4J" and the project file, "StrutsBC4J.jpr".

Run the BC4J Struts Starter Page wizard

  1. To get started, we will run the BC4J Struts Starter App Wizard. This wizard will create all of the the necessary items needed for a simple Struts application which has access to BC4J.
  2. Invoke the wizard:
    File | New ... | Struts Based JSP for Business Components for Java | Struts Starter Page



  3. Click "OK" to proceed.
  4. In step 1 of 2, Click on "New.." to define a new Data definition (cpx) file.
  5. In the BC4J Client Data Model Wizard, select the "...bc4j.jpr" project that you created in the last exercise.
  6. Accept the remaning defaults and click "Finish". This step allowed you to associate this project with the BC4J project. You should now be back in the Struts Starter page wizard.
  7. In step 2 of 2 in the Struts Starter app wizard name the JSP to "showemps.jsp" (instead of untitled1.jsp)
    and set the Action Path to "/showemps".


  8. Click Next and Finish to generate your application. You are almost done. The next step is to add some databinding to the empty JSP page.

Databinding the showemps.jsp Page

After generating the JSP and the necessary Struts files, we can now databind the empty showemps JSP page.

  1. You'll be using the new "JSP Databinding" UI instead of the Component Palette to add BC4J Tags to the page.
  2. To turn on the JSP DataBinding UI you must select it from the menu: View | JSP DataBinding
  3. In the top left window of the DataBinding UI, select "EmpView1". A set of usable controls should appear in the lower windows of the UI.
  4. Select the "RowSetIterate" control and drag and drop it to the JSP page. Make sure it is in the <body> portion of the page.



  5. You should now have the tag RowsetIterate on your page. Now in the body of the RowsetIterate tag, enter a line break <BR>
  6. It should look like this:
    <jbo:RowsetIterate datasource="Mypackage1Module.EmpView1" >
    <br>
    </jbo:RowsetIterate>
  7. After the Line break <br>, we will place a ShowValue tag for the employee name. In the Databinding UI, click on "Ename". (It is a child node of EmpView1.)
  8. In the middle window, select "ShowValue" and drag and drop it onto the JSP page just after the line break <br>.
    <jbo:RowsetIterate datasource="Mypackage1Module.EmpView1" >
    <br>
    <jbo:ShowValue datasource="Mypackage1Module.EmpView1" dataitem="Ename" ></jbo:ShowValue>

    </jbo:RowsetIterate>

  9. You can now run this small app to display the Employee names.

Running the Struts app using the ShowEmps Action

If we tried to run the page, showemps.jsp directly, it would show an error and fail. This is because a Struts BC4J app must have a usable BC4J Application Module available in the Web Context. This is done with the Struts Action that we generated earlier. So in order to properly run this page, we must run the /showemps action first.
When the action is run, it will establish a connection to a BC4J Application Module and then load it into the web Context. It will then proceed "Forward" to the showemps.jsp page which simply has to render the data.
This is a very clean way to preserve MVC in that the page itself does not have to have any non-presentation code which connects to the middle-tier. This is handled by the Struts Action.

  1. To run the showemps action we will simply locate the action in the file navigator (It is under the Struts-Config.xml node) and right-click run "/showemps".



  2. When the action executes it will forward to your databound page.





Exercise 3: Generating a BC4J Struts JSP Application

Now we will generate a complete working Struts-based application which uses BC4J.

  1. Create a new empty project in your workspace. Name the new project, "CompleteApp.jpr".
  2. In the navigator, right-click CompleteApp.jpr and select New... in the context menu.
  3. 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

  4. In the BC4J Struts JSP Application Wizard, Welcome page, click Next.
  5. 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
  6. In Step 2 and Step 3 accept all the defaults.
  7. In the final step, click Finish.
  8. Notice the new files that have been added to your project:
  9. Click the Save All button to save your work.
  10. 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 complete this step.
  11. To run the application, you can either right-click run the main.html or alternatively you could run one of the actions to view an individual page.
  12. To run the master-detail JSP page via a Struts action do the following:
    • In the navigator, right-click EmpForeignKeyDeptLink1 (under the struts-config.xml node). 
      Note: We are starting the application by invoking the Struts Action for the Emp/Dept foreign key JSP page. The name of this action/BC4J link may be defined slightly differently depending on how your foreign key is configured on your database.

    • Select Run EmpForeignKeyDeptLink1 in the context menu.


  13. When the browser loads your application, you will see a Master Detail page that allows you to navigate through the Depts and Emps



  14. After running the application, feel free to familiarize yourself on how the application works. Notice how there are actions defined for each page and that before a JSP can render BC4J data, it must have been called by the Struts action.

 

This concludes the Using Jakarta Struts with JDeveloper Hands-On exercises

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