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.
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.
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.
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.
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.
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.
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.
2.
Choose View | Component Palette
to open the Component Palette, if it is not already displayed.
3.
In the Component Palette, Click the Page Forward
icon and Click onto the diagram surface to create a page in the Design
editor.
4.
In the Design editor, change the highlighted default name
page1
to personForm.
5.
Double click the personForm
page and click OK to accept the jsp creation.
6.
In the Applications Navigator, double click the
ApplicationResources.properties node located in the ViewController
| Application Sources | view folder.
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 :
8.
Click on the personForm
tab on the top of the Editor window to open the Design
view of the JSP.
9.
Open the Component Palette and select
the Struts Bean library.
10.
Click on the message tag to add it on
the page, and press Enter Key to add a new line.
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.
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.
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.
14.
In the Component Palette, select the Struts Html
library, click the errors tag and press Enter
Key to add a new line.
15.
Click the form tag in the Component Palette.
16.
In the Tag Editor, for the action
attribute, select the personForm.do
from the drop down list as the value and click OK.
17.
Select the HTML tag library from the Component
Palette.
18.
Select the Table tag and specify 7
for the rows size and 2 for the columns size, then
OK.
19.
Your JSP should look like this:
20.
Click on the ApplicationResources.properties
tab on the top of the viewer window to open the Source viewer for this
file.
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
22.
Click on the personForm.jsp tab on top
of the editor to open the Design editor for the JSP.
23.
From the Struts Html library, select the
Reset tag and drag it from the Component Palette into
the last row - first column.
24.
Select the Submit Button
tag and drag it from the Component Palette onto the last row
- second column.
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.
2.
Drag and Drop a message tag in the first
column for the first 6 rows.
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.
4.
Repeat the previous operation for each of the 5 following
cells. and choose respectively the following key values:
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.
2.
Open the Structure pane. (View | Structure)
It should be located underneath the Applications Navigator,
and right click on the Struts Config node.
3.
Select the New | Form Beans option from
the context menu.
4.
Select the newly created Form Beans node,
right click on it and select New | Form Bean
from the context menu.
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.
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.
7.
Right click the person
bean in the Structure pane, select New | Form Property
from the context menu.
8.
In the Property Inspector, set the name
to firstname and the
type to java.lang.String
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.
Step 3 - Add an Action
and a JSP to the Application
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.
2.
In the Component Palette select
the Action icon and drag and drop it on the diagram
below the personForm.
3.
Change the name of the Action from action1
to proceed.
4.
Double click the proceed
icon to create the Struts Action. Accept the default Name,
Package and Extends values and click
OK.
5.
Accept the default generated Java code and close the Source
editor. Click OK to accept to save the file.
6.
In the Component Palette select the Page
Forward icon and drag and drop it on the diagram at the right
of the personForm.
7.
Change the name of the JSP from page1
to valid.
8.
Double click the valid JSP to open the
Design view.
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.
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.
11.
Open the ApplicationResources.properties
and add the following statement:
valid.ok = Well Done, Data
is Valid.
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.
13.
In the Component Palette, select the
page link and draw a line from valid
page to personForm page.
14.
Open the personForm JSP and select the
Source tab.
15.
In the Structure pane select the HTML
tag.
16.
In the Property Inspector, for the action
attribute select the proceed.do value from the drop
down list.
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.
18.
In the Structure Pane, select the
proceed node.
19.
Use the Property Inspector, for the proceed
action and specify the following values:
input
personForm.jsp
name
person
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'.
21.
The Page Flow diagram should look like this:
22.
Right click the personForm.jsp
in the Viewer and select the Run option.
23.
At this stage, you should be able to enter values in the
Person Form, and submit the form.
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.
Note: when returning to JDeveloper, ignore the error
messages that appear in the Log Window.
In the Applications Navigator,
select the struts-config.xml
node.
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.
3.
Select the new Plug In node and switch
to the Property Inspector.
4.
Set the className property for the plug-in
to org.apache.struts.validator.ValidatorPlugIn
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.
6.
Select the new entry and switch to the Property Inspector.
7.
Set the property property for this entry
in the inspector to pathnames
8.
Set the value property to the following:
/WEB-INF/validator/validator-rules.xml,/WEB-INF/validator/validations.xml
9.
Choose File | Save All
to save all your work thus far.
Copy the Struts Validator file
in the project directory
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.
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
3.
Switch back to JDeveloper and click on
the WEB-INF node in the
Applications Navigator.
4.
Select File | Open, in the dialog, click
on the validator folder
and select the validator-rules.xml
file. Click Open.
5.
The new entry appears in the Applications Navigator.
6.
In the Applications Navigator right click
on the validator node
and select New from the context menu.
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.
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.
9.
Click OK to create the new validations.xml
file in the project.
10.
Double click the validator-rules.xml
file to open it in the editor.
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.
12.
Open the ApplicationResources.properties
file and paste the copied messages at the bottom of the file.
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:
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
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
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.
13.
Entering values should display the following page:
14.
Here is an example of what you get when entering invalid
values.
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