STRUTS VALIDATION FRAMEWORK
There is more info on Validator over at the jakarta-commons/validator wiki http://wiki.apache.org/jakarta-commons/ValidatorSetup
Some of the most common UI input validation includes, checking for mandatory fields, email validation, credit card validation, etc. The Validator framework is a great way of validating your UI input.
The core of the Validator framework comes from jakarta-commons where, much like the other components, it is maintained as a separate, independent project that does not require Struts. The Validator Plugin allows us to take advantage of that library in a Struts-familiar way.
There are 2 different ways to use the validator in Struts. Form-based validation and Action-based validation. The differences are explained in detail in the users guide, but we'll give a quick summary here.
- Form-based validation is validation that gets applied to a particular form name. You can have different validations on the same form, if you specify a different name.
- Action-based validation is validation that gets applied for a specific path (such as "/createCustomer.do").
Here are some necessary step before we can begin using the struts validation framework with "form-based" validation (as described above).
- Create an action form class that extends org.apache.struts.validator.ValidatorForm. 2. Create an action class. 3. Set up the resource bundle for the application in struts config file. 4. Create a jsp page that serves as an input. 5. Setup struts-config.xml 6. Copy the validator-rules.xml and validator.xml into WEB-INF. 7. Add the form to the form set in the validator.xml
Step 1. Create an action Form class
This form will contain one field called user name field, that is mandatory.
Step 2. Create a action class
Step 3. Set up the resource bundle
Specify a resource bundle key-value pair
Step 4. Create a JSP as an input
We name this JSP as input.jsp
Step 5. Map the jsp page with the action form and the action class inside struts-config.xml.
Step 7. Add the form to the form set in the validator.xml
the depends attribute specify what validation rules that we should apply to the userName field on the input.jsp. Here we specify required, which mean everytime the user forgot to key in something in the userName field, the validator framework will alert the user that they have not key in any value in the field.