8. Validating Form Input Data

In this section you will use the Struts Validation Framework Wizard to add checks for the fields entered in the login page.


With the Validator Framework, Struts introduced a very extensible data validation system. But because several components make up the Validator Framework (validators, configuration files, jsp validator form classes, and Resource Bundles) keeping track of these artifacts can be a cumbersome process. The Workshop Validation Framework Wizard allows for the creation, modification, and synchronization of all validation resources through a single interface. As new validations are added to the application through the Validation Framework Wizard, all of the changes propagate to the appropriate configuration files.

Loading the Validation Framework Wizard

  1. In AppXplorer, locate and expand the branches for Struts Configuration > Modules > Default to display the list of Struts artifacts.
  2. Open Validation Forms and double-click logonForm.
  3. This will load the validation.xml located in the WEB-INF directory into the Validation Framework Editor. From within this editor, you can easily modify, delete, and add new form validations. In the next section we will be adding a new validation.

Creating a new Form Validation

  1. In the Forms section of the Validation Editor, right-click logonForm and select Delete.
  2. To recreate the logonForm validation, click the New Form button to launch the validation wizard.
  3. The validation wizard graphically walks through the creation of form validations and, based on the user inputs, adds the appropriate source code to the validation.xml.

  4. From the Insert Form Validation window, select logonForm and click Next.
  5. Add validation for both the password and username fields. Click Next.
  6. No resource key pattern has been defined for the application so click Next to define the validators for the password field.
  7. For the password field, click Add Field Validators and select minlength and required. Then click Ok.
  8. In the Field Validators window for the password field, set the Min Length to 3 and click the {0} symbol to select the output message. This will display the list of possible resource names to display.
  9. From the list of resource keys, choose login.password.text, select Ok, and then click Next.
  10. To add validation for the username field add the 'required' field validator.
  11. Click the {0} symbol to set the output message for this validator and then choose the login.login.text resource key. Click Ok and then Finish.

Adding Additional Validators

  1. New Validators can also be defined and edited in the Validation Framework Wizard. To view the current list of custom Validators, select the Validators tab (at the bottom of the Source view).
  2. The project does not contain custom validators but additional validators can be easily added by clicking the New Validator button and using the Validator Wizard.

Related tasks

Viewing included pages
Viewing web pages in a different locale

Click one of the following arrows to navigate through the tutorial:

Still need help? Post a question on the Workshop newsgroup.