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:
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
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
- In JDeveloper, create a new Workspace or use an existing one. (The name
of Workspace is not important.)
- In your Workspace create a new project called SimpleStruts.jpr
File | New... | General | Empty Project
- Name the directory, "SimpleStruts" and the project file, "
SimpleStruts.jpr".
- 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.

- Click Yes to run the Struts starter wizard.
- 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.
- 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.
- 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.
-
Name the page, login.jsp.
-
You should now have a new login JSP file added to your project and invoked
in the editor.
-
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.
-
Change the JSP's default header to:
<h2>Login Page</h2>
-
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 />
- 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">
-
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.
- 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".
- Add another prompt for the password field in a similar fashion. This time
set the key to "password.prompt".
- 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 />
- 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>
- 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.)
- 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.
- 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)
- 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
- 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.
-
Enter the following fields:

Note: The package name is whatever your default package name is for the
project.
- 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.
- Invoke the Class editor on the Login Form bean:
- Once the class appears, click on the "Fields" tab at the bottom
of the class editor window.
- Add a new field for "userid" by clicking on the "+ Add"
button in the class editor.
- Set the field name to "userid", accept the remaining defaults
and click "OK".
- Repeat the steps to create a field for "password". Your fields
should look like:
- Notice the getter and setter methods have been added to the bean as well.
- 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;
}
- Select File | Save All and close the class and
code editor windows.
Building a Struts Action
- 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.
- Enter the following fields:
- 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.
- Select File | Save All.
- 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
- Invoke the Struts-config editor by right-clicking
on the Struts-config.xml file and selecting "Edit Struts-Config"
- Once the Struts-Config editor appears, click on the "Action Mappings"
node on the left. You will see your "/login" action.
- Now click on the "/login" action in the Action Mappings window.
- Click on the "Form Bean" tab in the Properties window below.
- Select your "LoginForm" from the dropdown list for "Name:"
- Make sure the Validate() checkbox is checked.
- Enter "login.jsp" in the "input" field.
- 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:
- Now we will add the Global Forwards "success" and "failure"
that we referred to in our Action.
- Re-invoke the Struts-Config editor. This time we will add some Global Forwards.
- In the Struts-Config editor click on "Global Forwards" node on
the left tree.
- Click on the "Add" button on the right.
- Name the new Forward, "success" and associate it to the path "/success.jsp".
- Now repeat the same step for "failure" forward and specify "/failure.jsp"
as its path.
- Click "OK" to dismiss the Struts-Config Editor.
- 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.
- Edit the ApplicationResources.properties.
- 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..
- Select File | Save All.
- Close the editor. You will now be able to run the application.
- Run the application by right-clicking on the login.jsp file and selecting
Run login.jsp.
- 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.
- Now enter incorrect login information: bogus/bogus...
- You should see the Failure page appear!
- 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.
- In the same workspace create a new Business Components project using the
Business Components Project Wizard:
File | New... | General | Projects | Project Containing Business Components...
- Name the directory, bc4j and the project file, bc4j.jpr.
- As the project is created, the Business Components wizards is automatically
invoked.
- Accept the defaults. Note: You must have a connection created for a database
containing the scott/tiger schema.
- On the final step of the BC4J Wizard, select Emp and Dept to create ViewObject/Entity
Objects.

- Click Finish to generate your Business Components.
- Now Save and compile your project bc4j.jpr.
Exercise 2: Building a Simple BC4J Struts Page
Create a StrutsBC4J project
- Create a new project called StrutsBC4J.jpr
File | New... | General | Empty Project
- Name the directory, "StrutsBC4J" and the project file, "StrutsBC4J.jpr".
Run the BC4J Struts Starter Page wizard
- 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.
- Invoke the wizard:
File | New ... | Struts Based JSP for Business Components for Java | Struts
Starter Page
- Click "OK" to proceed.
- In step 1 of 2, Click on "New.." to define a new Data definition
(cpx) file.
- In the BC4J Client Data Model Wizard, select the "...bc4j.jpr"
project that you created in the last exercise.
- 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.
- 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".

- 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.
- You'll be using the new "JSP Databinding" UI instead of the Component
Palette to add BC4J Tags to the page.
- To turn on the JSP DataBinding UI you must select it from the menu: View
| JSP DataBinding
- 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.
- Select the "RowSetIterate" control and drag and drop it to the
JSP page. Make sure it is in the <body> portion of the page.

- You should now have the tag RowsetIterate on your page. Now in the body
of the RowsetIterate tag, enter a line break <BR>
- It should look like this:
<jbo:RowsetIterate datasource="Mypackage1Module.EmpView1"
>
<br>
</jbo:RowsetIterate>
- 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.)
- 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>
- 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.
- 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".
- 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.
- Create a new empty project in your workspace. Name the new project, "CompleteApp.jpr".
- In the navigator, right-click CompleteApp.jpr and select New...
in the context menu.
- 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
- In the BC4J Struts JSP Application Wizard, Welcome page, click Next.
- 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
- In Step 2 and Step 3 accept all the defaults.
- In the final step, click Finish.
- Notice the new files that have been added to your project:
- Click the Save All button to save your work.
- 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.
- 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.
- 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.

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

- 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
|