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

Purpose

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

Time to complete

Approximately 1 hour


Viewing Screenshots

 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

Scenario

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 %JDEV_HOME%jdev/mywork/StrutsValidator/ViewController/public_html/WEB-INF/ and create a new folder named validator.

Move your mouse over this icon to see the image

 

2.

Copy the following file %JDEV_HOME%/jakarta-struts/lib/validator-rules.xml to the %JDEV_HOME%jdev/mywork/StrutsValidator/ViewController/public_html/WEB-INF/validator directory

Move your mouse over this icon to see the image

 

3.

Switch back to JDeveloper and click on the WEB-INF node in the Applications Navigator.

Move your mouse over this icon to see the image

 

4.

Select File | Open, in the dialog, click on the validator folder and select the validator-rules.xml file. Click Open.

Move your mouse over this icon to see the image

 

5.

The new entry appears in the Applications Navigator.

Move your mouse over this icon to see the image

 

6.

In the Applications Navigator right click on the validator node and select New from the context menu.

Move your mouse over this icon to see the image

 

7.

In the New Gallery expand the General node, choose XML as the Categories and in the Items list double click the XML Document entry. Click OK.

Move your mouse over this icon to see the image

 

8.

In the Create XML File dialog enter validations.xml as the name and click on the Browse button for the directory to reach the %JDEV_HOME%jdev/mywork/StrutsValidator/ViewController/public_html/WEB-INF/validator directory. Click OK.

Move your mouse over this icon to see the image

 

9.

Click OK to create the new validations.xml file in the project.

Move your mouse over this icon to see the image

 

10.

Double click the validator-rules.xml file to open it in the editor.

Move your mouse over this icon to see the image

 

11.

Copy the following predefined Validator error messages.

# Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.

errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.

errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.

Move your mouse over this icon to see the image

 

12.

Open the ApplicationResources.properties file and paste the copied messages at the bottom of the file.

Move your mouse over this icon to see the image

 

 

Add your own validation rules

Back to List

 

1.

In the Applications Navigator, open the validations.xml file.

Move your mouse over this icon to see the image

 

2.

Copy and paste the following tags. Format and validations rules need to be specified between, the <form> and </form> tags

<form-validation>
 <formset>

   <!-- Start of Format mask for birthdate and phone Numbers -->
   .
   .
   .
   .
   <!-- End of Format mask for birthdate and phone Numbers -->

   <!-- Start of validation rules for each bean -->

<form name="person">
   .
   .
   .
   .
   <!-- End of validation rules for each bean -->

  </form>
 </formset>
</form-validation>

Move your mouse over this icon to see the image

 

3.

After the <!-- Start of Format... comment, cut and paste the following statements to specify the Date format for the birthdate bean:

<constant>
 <constant-name>dateFormat</constant-name>
 <constant-value>yyyy-MM-dd</constant-value>
</constant>

Move your mouse over this icon to see the image

 

4.

Below the previous statements, cut and paste the following statements to specify the phone number format for the phone bean:

<constant>
 <constant-name>phoneFormatMask</constant-name>
 <constant-value>^\d{3}[.]\d{3}[.]\d{4}$</constant-value>
</constant>

Move your mouse over this icon to see the image

 

5.

After the <!--Start of validation... comment and below the <form name="person"> tag, cut and paste the following statements to specify the Required rule for the firstname field:

<field property="firstname" depends="required">
 <arg0 key="validation.error.firstName"/>
</field>

Move your mouse over this icon to see the image


6.

Below the previous instructions, cut and paste the following statements to specify the Required rule for the lastname bean:

<field property="lastname" depends="required">
 <arg0 key="validation.error.lastname"/>
</field>

Move your mouse over this icon to see the image

 

7.

Below the previous instructions, cut and paste the following statements to support the validation of the birthdate bean:

<field property="birthdate" depends="date">
 <arg0 key="validation.error.birthdate"/>
  <var>
  <var-name>datePatternStrict</var-name>
  <var-value>${dateFormat}</var-value>
  </var>
</field>

Move your mouse over this icon to see the image

 

8.

Below the previous instructions, cut and paste the following statements to support the validation of the age bean where the field age:
- is required if birthdate is null,
- must be an integer
- must be between 1 and 100

<field property="age" depends="requiredif,range,integer">
<msg name="requiredif" key="validation.error.age.miss"/>
<msg name="integer" key="validation.error.age.int"/>
<arg0 key="validation.error.age"/>
 <var>
  <var-name>field[0]</var-name>
  <var-value>birthdate</var-value>
 </var>
 <var>
  <var-name>fieldTest[0]</var-name>
  <var-value>EQUAL</var-value>
 </var>
 <var>
  <var-name>fieldValue[0]</var-name>
  <var-value></var-value>
  </var>
  <arg0 key="validation.error.age"/>
 <arg1 key="${var:min}" resource="false"/>
  <arg2 key="${var:max}" resource="false"/>
  <var>
   <var-name>min</var-name>
  <var-value>1</var-value>
 </var>
  <var>
  <var-name>max</var-name>
 <var-value>100</var-value>
</var>
</field>

Move your mouse over this icon to see the image

 

9.

Below the previous instructions, cut and paste the following statements to support the validation of the phone bean:

<field property="phone" depends="mask">
 <msg name="mask" key="validation.error.phone.format"/>
 <arg0 key="validation.error.phone"/>
 <var>
  <var-name>mask</var-name>
  <var-value>${phoneFormatMask}</var-value>
 </var>
</field>

Move your mouse over this icon to see the image

 

10.

Below the previous instructions, cut and paste the following statements to support the validation of the email bean:

<field property="email" depends="email">
 <arg0 key="validation.error.email"/>
</field>

Move your mouse over this icon to see the image

 

11.

Open the ApplicationResources.properties file and add the following error messages:

validation.error.firstName=Error Person's first name
validation.error.lastName=Error Person's last name
validation.error.phone=Error Person's phone number
validation.error.phone.format={0} should be in the format nnn.nnn.nnnn
validation.error.email=Error Person's Email address
validation.error.birthdate=Error Person's date of birth
validation.error.age.customError=Error Person's age
validation.error.age.miss=Birthdate is Missing
validation.error.age=Error Person 'age
validation.error.age.int=Age is not an Integer

Move your mouse over this icon to see the image

 

12.

You are now ready to test the whole application. Open the struts Page Flow diagram, right-click on the personForm.jsp and choose Run from the context menu.

Move your mouse over this icon to see the image

 

13.

Entering values should display the following page:

Move your mouse over this icon to see the image

 

14.

Here is an example of what you get when entering invalid values.

Move your mouse over this icon to see the image

 

 

 

Enable JavaScript validation

Back to List

 

1.

Select the personForm tab and open the Source editor.

Move your mouse over this icon to see the image

 

 

2.

Above the </head> tag, add the following statement:

<html:javascript formName="person"/>

Move your mouse over this icon to see the image

 

3.

Locate the <html:form action="proceed.do"> tag and update the statement to the following one:

<html:form action="proceed.do" onsubmit="return validatePerson(this)">

Move your mouse over this icon to see the image

 

4.

Select the personForm entry in the Applications Navigator and select the Run option from the context menu.

Move your mouse over this icon to see the image

 

5.

Try to submit the personForm with no values in to test the JavaScript validation.

Move your mouse over this icon to see the image

 

 

Summary

Back to Topic List

In this tutorial you've learned how to:

 Create a Struts Based Application workspace
 Create a JSP for a Person Form
 Add an Action and a JSP to the application
 Add the Struts Validation Components

 

Related topics

 

 Move your mouse over this icon to hide all screenshots

Left Curve
Popular Downloads
Right Curve