Legal | Privacy

Creating an Input Form JSP Page Using the Struts Validator to Validate Data Entry

This tutorial describes how you can set up a project in Oracle JDeveloper 10g to use the Validator plug-in provided with Apache Struts. The Struts Validator is an extension to Struts which allows you to define declarative input validation using rules defined in XML, rather than having to code Form Beans with custom validation code. This in turn can mean that there is less requirement to code specialized Form Beans for handling page input, as the Validator can be successfully used with dynamic Form Beans, defined wholly in XML, using the Struts DynaActionForm.

Topics

This lesson will discuss the following:

Overview
Step 1 - Create a Struts Based Project

Step 2 - Create a JSP for a Person Form

Step 3 - Add an Action and a JSP to the Application
Step 4 - Add the Struts Validation Components
Summary

Approximately 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

The goal of this tutorial is to teach you how to build a JSP page that uses the Struts Validator plug-in for input validation. The Validator plug-in is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.

Back to Topic List

The JSP page you create in this tutorial will display a fictional Person Form to illustrate the use of some of the predefined validation rules that you can use in a Page.

 

Step 1 - Create a Struts Based Project

Back to Topic List

When you work in JDeveloper, you organize your work in projects within application workspaces. JDeveloper provides a number of predefined templates which allow you to create a workspace and projects that are configured for developing the types of application you are working on. The templates provide the basic range of technologies that are needed, and you create your working environment by selecting the one that fits your needs, then configuring it to add any additional technologies you are going to use. The options that are available to you in the New Gallery and for some context menu operations depend on your choice of template for that workspace.

In this tutorial you are going to choose the default web application template.

Create a new Application Workspace with model and view projects

In these steps you will create the Application environment for your development.

1.

In the Applications Navigator, right-click Applications and choose New Application Workspace from the context menu.

Move your mouse over this icon to see the image

 

2.

In the Create Application Workspace dialog, rename the application Application1 to StrutsValidator.
(Notice that the directory name changes to match the new name.) For the application template, select Web Application [Default] , and click OK.

Move your mouse over this icon to see the image

 

3.

The StrutsValidator workspace is created and displayed as a node in the navigator.
Save your work by clicking on the Save All button. You should save your work at regular intervals as you work through the rest of this tutorial.

Move your mouse over this icon to see the image

 

Back to List

 

Step 2 - Create a JSP for a Person Form

Back to Topic List

In these steps you will create a JSP page for an Input Form. The procedure shows how to create a form using the Component Palette.

Create a JSP
Set the labels for the fields
Set the text fields
Create the Form Beans for input Fields

Create a JSP

Back to List

At the end of this step, your JSP should look like this:

1.

In the Applications Navigator, expand the ViewController > Web Content > WEB-INF node and double click the struts-config.xml node to open the Page Flow diagram.

Move your mouse over this icon to see the image

 

2.

Choose View | Component Palette to open the Component Palette, if it is not already displayed.

Move your mouse over this icon to see the image

 

3.

In the Component Palette, Click the Page Forward icon and Click onto the diagram surface to create a page in the Design editor.

Move your mouse over this icon to see the image

 

4.

In the Design editor, change the highlighted default name page1 to personForm.

Move your mouse over this icon to see the image

 

5.

Double click the personForm page and click OK to accept the jsp creation.

Move your mouse over this icon to see the image

 

6.

In the Applications Navigator, double click the ApplicationResources.properties node located in the ViewController | Application Sources | view folder.

Move your mouse over this icon to see the image

 

7.

In the Source editor add new messages to the ApplicationResources.properties, cut and paste the following statements at the bottom of the file, and save your work.

person.title=Personal Record Form
person.subtitle=Enter the person information below :

Move your mouse over this icon to see the image

 

8.

Click on the personForm tab on the top of the Editor window to open the Design view of the JSP.

Move your mouse over this icon to see the image

 

9.

Open the Component Palette and select the Struts Bean library.

Move your mouse over this icon to see the image

 

10.

Click on the message tag to add it on the page, and press Enter Key to add a new line.

Move your mouse over this icon to see the image

 

11.

Double click the message tag to open the Tag Editor and for the key attribute select the person.title value from the drop down list.

Move your mouse over this icon to see the image

 

12.

In the Component Palette, click on the message tag to add it on the following line and press Enter Key to add a new line.

Move your mouse over this icon to see the image

 

13.

Double click the message tag to open the Tag Editor and for the key attribute select the person.subtitle value from the drop down list.

Move your mouse over this icon to see the image

 

14.

In the Component Palette, select the Struts Html library, click the errors tag and press Enter Key to add a new line.

Move your mouse over this icon to see the image

 

15.

Click the form tag in the Component Palette.

Move your mouse over this icon to see the image

 

16.

In the Tag Editor, for the action attribute, select the personForm.do from the drop down list as the value and click OK.

Move your mouse over this icon to see the image

 

17.

Select the HTML tag library from the Component Palette.

Move your mouse over this icon to see the image

 

18.

Select the Table tag and specify 7 for the rows size and 2 for the columns size, then OK.

Move your mouse over this icon to see the image

 

19.

Your JSP should look like this:

Move your mouse over this icon to see the image

 

20.

Click on the ApplicationResources.properties tab on the top of the viewer window to open the Source viewer for this file.

Move your mouse over this icon to see the image

 

21.

In the Source editor add new messages to the ApplicationResources.properties. Cut and paste the following statements at the bottom of the file.

person.firstname=First Name :
person.lastname=Last Name :
person.birthdate=Date of Birth (YYYY-MM-DD):
person.age=Age (required id Date of Birth is null):
person.phone=Phone Number (XXX.XXX.XXXX):
person.email=Email Address :

person.explain.1=* is for a mandatory field

Move your mouse over this icon to see the image

 

22.

Click on the personForm.jsp tab on top of the editor to open the Design editor for the JSP.

Move your mouse over this icon to see the image

 

23.

From the Struts Html library, select the Reset tag and drag it from the Component Palette into the last row - first column.

Move your mouse over this icon to see the image

 

24.

Select the Submit Button tag and drag it from the Component Palette onto the last row - second column.

Move your mouse over this icon to see the image

 

 

 

Set the Labels for the fields

Back to List

In this step, you add message tags for the labels of the input form. At the end of this step, your JSP should like like this:

1.

Select the Struts Bean library from the component Palette.

Move your mouse over this icon to see the image

 

2.

Drag and Drop a message tag in the first column for the first 6 rows.

Move your mouse over this icon to see the image

 

3.

Click on the message tag in the top left cell of the table, using the Property Inspector for the key attribute select the person.firstname value from the drop down list.

Move your mouse over this icon to see the image

 

4.

Repeat the previous operation for each of the 5 following cells. and choose respectively the following key values:

person.lastname
person.birthdate
person.age
person.phone
person.email

Move your mouse over this icon to see the image

 

5.

In the top left cell, next to the person.firstname message tag enter ' *' (a space and an asterisk).

Move your mouse over this icon to see the image

 

6.

Select the '*' character and set the foreground to a red color of your choice.

Move your mouse over this icon to see the image

 

7.

Repeat the last two operations for the person.lastname message.

Move your mouse over this icon to see the image

 

8.

Add a new line at the bottom of the page and drag and drop a message tag below the table.

Move your mouse over this icon to see the image

 

9.

Click on the new message tag, and using the Property Inspector, for the key attribute select the person.explain.1 value from the drop down list.

Move your mouse over this icon to see the image

 

10.

Select the message at the top of the JSP (person.title)

Move your mouse over this icon to see the image

 

11.

Select the Heading 3 for that message.

Move your mouse over this icon to see the image

 

12.

Your JSP should now look like this:

Move your mouse over this icon to see the image

 

Set the text fields

Back to List

In this step, you add text fields for data entry. At the end of this step, your JSP should like like this:

1.

Select the Struts Html tag library from the Component Palette.

Move your mouse over this icon to see the image

 

2.

Drag a text tag and drop it in the top right cell of the table.

Move your mouse over this icon to see the image

 

3.

For the attribute property of the Tag Editor, type firstname as the value.

Move your mouse over this icon to see the image

 

4.

Repeat the operation for the next 5 cells in the same column and specify respectively the following property values in the Tag Editor:

lastname
birthdate
age
phone
email

Move your mouse over this icon to see the image

 

5.

Your JSP should look like this.

Move your mouse over this icon to see the image

 

Create the Form Beans for Input Fields

Back to List

In this step, you create the form bean components that will hold the values entered in the input fields.

1.

Select the struts-config.xml file in the Applications Navigator.

Move your mouse over this icon to see the image

 

2.

Open the Structure pane. (View | Structure) It should be located underneath the Applications Navigator, and right click on the Struts Config node.

Move your mouse over this icon to see the image

 

3.

Select the New | Form Beans option from the context menu.

Move your mouse over this icon to see the image

 

4.

Select the newly created Form Beans node, right click on it and select New | Form Bean from the context menu.

Move your mouse over this icon to see the image

 

5 .

Select the new bean, in the Structure pane and then using the Property Inspector set the name property to person. If the Property Inspector is not visible, you can use (CTRL+Shift+I) key combination.

Move your mouse over this icon to see the image

 

6.

In the Property Inspector, click in the type property, Click the Browse icon to browse the available types, and in the pop up window open the org | apache | struts | validator nodes and select the DynaValidatorForm. Click OK.

Move your mouse over this icon to see the image

 

7.

Right click the person bean in the Structure pane, select New | Form Property from the context menu.

Move your mouse over this icon to see the image

 

8.

In the Property Inspector, set the name to firstname and the type to java.lang.String

Move your mouse over this icon to see the image

 

9.

Repeat the last couple of operations with the following characteristics for name and type properties:

Name
Type
lastname
java.lang.String
birthdate
java.lang.String
age
java.lang.String
phone
java.lang.String
email
java.lang.String

The new elements appear in the Structure pane.

Move your mouse over this icon to see the image

 

Step 3 - Add an Action and a JSP to the Application

Back to Topic List

The following steps show you how to add an Action that will process the JSP input fields and will return a JSP for valid input.

Create an Action

In this step you create an Action that receives the personForm jsp input.

1.

Click on the struts-config.xml tab to display the page flow diagram.

Move your mouse over this icon to see the image

 

2.

In the Component Palette select the Action icon and drag and drop it on the diagram below the personForm.

Move your mouse over this icon to see the image

 

3.

Change the name of the Action from action1 to proceed.

Move your mouse over this icon to see the image

 

4.

Double click the proceed icon to create the Struts Action. Accept the default Name, Package and Extends values and click OK.

Move your mouse over this icon to see the image

 

5.

Accept the default generated Java code and close the Source editor. Click OK to accept to save the file.

Move your mouse over this icon to see the image

 

6.

In the Component Palette select the Page Forward icon and drag and drop it on the diagram at the right of the personForm.

Move your mouse over this icon to see the image

 

7.

Change the name of the JSP from page1 to valid.

Move your mouse over this icon to see the image

 

8.

Double click the valid JSP to open the Design view.

Move your mouse over this icon to see the image

 

9.

In the Component Palette, select the Struts Bean library and drop a message tag on the page. Then hit Enter key to add a new line.

Move your mouse over this icon to see the image

 

10.

Double click the message tag to open the Tag Editor dialog. For the key attribute, in the value field enter: valid.ok, then OK.

Move your mouse over this icon to see the image

 

11.

Open the ApplicationResources.properties and add the following statement:

valid.ok = Well Done, Data is Valid.

Move your mouse over this icon to see the image

Save your work.

 

12.

Display the Page flow diagram and from the Component Palette, select the Forward icon, and draw a line from the proceed action to the valid page.

Move your mouse over this icon to see the image

 

13.

In the Component Palette, select the page link and draw a line from valid page to personForm page.

Move your mouse over this icon to see the image

 

14.

Open the personForm JSP and select the Source tab.

Move your mouse over this icon to see the image

 

15.

In the Structure pane select the HTML tag.

Move your mouse over this icon to see the image

 

16.

In the Property Inspector, for the action attribute select the proceed.do value from the drop down list.

Move your mouse over this icon to see the image

 

17.

Double click the struts-config.xml node in the Applications Navigator, open the Source view and in the Structure pane open the Action Mappings node.

Move your mouse over this icon to see the image

 

18.

In the Structure Pane, select the proceed node.

Move your mouse over this icon to see the image

 

19.

Use the Property Inspector, for the proceed action and specify the following values:

input
personForm.jsp
name
person

Move your mouse over this icon to see the image

 

20.

Click on the Design tab at the bottom of the Viewer to display the page flow diagram for the struts-config.xml file. And Right-click on the personForm icon to choose the 'Refresh Diagram from Page option'.

Move your mouse over this icon to see the image

 

21.

The Page Flow diagram should look like this:

Move your mouse over this icon to see the image

 

22.

Right click the personForm.jsp in the Viewer and select the Run option.

Move your mouse over this icon to see the image

 

23.

At this stage, you should be able to enter values in the Person Form, and submit the form.

Move your mouse over this icon to see the image

 

24.

However you can enter any value (valid or not) or not enter any value, the behavior is the same, leading you to the Valid page.

Move your mouse over this icon to see the image

Note: when returning to JDeveloper, ignore the error messages that appear in the Log Window.

 

Step 4- Add the Struts Validation Components

Back to Topic List

Add the Struts Validator Plug In
Copy the Struts Validator file in the project directory
Add your own validation rules
Enable JavaScript validation

Add the Struts Validator Plug-in

Back to List

 

1.

In the Applications Navigator, select the struts-config.xml node.

Move your mouse over this icon to see the image

 

2.

In the structure pane, right click on the the top level Struts Config node and choose New > Plug In from the context menu. This creates a new Plug In node in the structure.

Move your mouse over this icon to see the image

 

3.

Select the new Plug In node and switch to the Property Inspector.

Move your mouse over this icon to see the image

 

4.

Set the className property for the plug-in to org.apache.struts.validator.ValidatorPlugIn

Move your mouse over this icon to see the image

 

5.

Right click the Plug In node in the Structure pane and choose New > Set Property from the context menu. This creates a child node under the Plug In node.

Move your mouse over this icon to see the image

 

6.

Select the new entry and switch to the Property Inspector.

Move your mouse over this icon to see the image

 

7.

Set the property property for this entry in the inspector to pathnames

Move your mouse over this icon to see the image

 

8.

Set the value property to the following:
/WEB-INF/validator/validator-rules.xml,/WEB-INF/validator/validations.xml

Move your mouse over this icon to see the image

 

9.

Choose File | Save All to save all your work thus far.

Move your mouse over this icon to see the image

 

 

Copy the Struts Validator file in the project directory

Back to List

 

1.

Switch to your operating system (the following example illustrates the operations in a MS/Windows environment) to locate the following directory