testcontent
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.
This lesson will discuss the following:
| |
Overview | |
| |
Step 1 - Create a Struts Based Project | |
| |
||
| |
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
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.
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.
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.
|
| 3. |
The
StrutsValidator workspace is created and displayed as a node in the navigator.
|
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 | |
At the end of this step, your JSP should look like this:
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: person.lastname
|
| 5. |
In the top left cell, next to the person.firstname message tag enter ' *' (a space and an asterisk).
|
| 6. |
Select the '*' character and set the foreground to a red color of your choice.
|
| 7. |
Repeat the last two operations for the person.lastname message.
|
| 8. |
Add a new line at the bottom of the page and drag and drop a message tag below the table.
|
| 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.
|
| 10. |
Select the message at the top of the JSP ( person.title)
|
| 11. |
Select the Heading 3 for that message.
|
| 12. |
Your JSP should now look like this:
|
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.
|
| 2. |
Drag a text tag and drop it in the top right cell of the table.
|
| 3. |
For the attribute property of the Tag Editor, type firstname as the value.
|
| 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
|
| 5. |
Your JSP should look like this.
|
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
|
||||||||||||
| 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:
The new elements appear in the Structure pane.
|
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.
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:
|
||||
| 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.
|
| |
Add the Struts Validator Plug In | |
| |
Copy the Struts Validator file in the project directory | |
| |
Add your own validation rules | |
| |
Enable JavaScript validation | |
| 1. |
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:
|
| 9. |
Choose
File | Save All to save all your work thus far.
|
| 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.
|
| 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.byte={0} must be a byte.
errors.date={0} is not a date.
|
| 12. |
Open the ApplicationResources.properties file and paste the copied messages at the bottom of the file.
|
| 1. |
In the Applications Navigator, open the validations.xml file.
|
| 2. |
Copy and paste the following tags. Format and validations rules need to be specified between, the <form> and </form> tags <form-validation>
<!-- Start of Format mask for birthdate and phone Numbers -->
<!-- Start of validation rules for each bean --> <form name="person">
</form>
|
| 3. |
After the <!-- Start of Format... comment, cut and paste the following statements to specify the Date format for the birthdate bean: <constant>
|
| 4. |
Below the previous statements, cut and paste the following statements to specify the phone number format for the phone bean: <constant>
|
| 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">
|
| 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">
|
| 7. |
Below the previous instructions, cut and paste the following statements to support the validation of the birthdate bean: <field property="birthdate" depends="date">
|
| 8. |
Below the previous instructions,
cut and paste the following statements to support the validation of the age bean where the field age:
<field property="age" depends="requiredif,range,integer">
|
| 9. |
Below the previous instructions, cut and paste the following statements to support the validation of the phone bean: <field property="phone" depends="mask">
|
| 10. |
Below the previous instructions, cut and paste the following statements to support the validation of the email bean: <field property="email" depends="email">
|
| 11. |
Open the ApplicationResources.properties file and add the following error messages: validation.error.firstName=Error Person's first name
|
| 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.
|
| 1. |
Select the personForm tab and open the Source editor.
|
| 2. |
Above the </head> tag, add the following statement: <html:javascript formName="person"/>
|
| 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)">
|
| 4. |
Select the personForm entry in the Applications Navigator and select the Run option from the context menu.
|
| 5. |
Try to submit the personForm with no values in to test the JavaScript validation.
|
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