Using the Struts 2 Validation Framework
Introduction
In the previous Struts 2 examples I used the a validate method in my ActionSupport classes (for example see the Register class in that example project) to do some simple validation of the input the user provided on the register.jsp web page. However, Struts 2 provides a comprehensive validation framework that makes it simple to integrate complex validation of user input without cluttering up the ActionSupport class.
Example of Using The Struts 2 Validation Framework
You can download an archived MyEclipse web project (reference 3) that demonstrates how to use the Struts 2 validation framework. To view a working example on the web, click here. In that project look at the Register.java class (package edu.ku.si.action), where you'll see that I removed the validate method. By using the Struts 2 validation framework I can keep my ActionSupport classes focused and separate out the validation of the user's input. I can also take advantage of validators that come with the Struts 2 validation framework.
Struts 2 comes with several built-in validators, including ones to check if the user has entered data into a required field, to check the format of an email address, and to check that a number is within a specific range. To use the Struts 2 validation framework and these validators you need to add an xml file that contains nodes which associate the form's fields with the Struts 2 validators that should be used to check the user's input.
In the example project under package edu.ku.si.action is an xml file named Register-validation.xml. To use the Struts 2 validation framework, you create an xml file that starts with the same name as the ActionSupport class that is executed when the user submits the form. In my example, the Register class is executed so the xml file is named Register-validation.xml. This xml file should be saved in the same location as the ActionSupport class. (Note there are other ways to setup your Struts 2 application to use the validation framework, consult the references).
If you open this file you'll see nodes like the following:
<field name="personBean.lastName">
<field-validator type="requiredstring">
<message>Last name is required.</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">40</param>
<param name="minLength">1</param>
<message>Your last name must be between 1 and 40 characters long.</message>
</field-validator>
</field>
The field node is used to specify which form field you want to validate. The field-validator child node is used to specify which validator you want to use to check that field's value. Multiple field-validators can be used to check a field. In the node above I'm specifying that the value the user entered into the personBean.lastName field should be checked by the requiredstring and stringlength validators. Consult the reference for the other built-in validators.
Note the message child node. Its text value will be displayed on register.jsp page just above the form field that failed validation. If any field fails validation the user will be returned to the view designated by the name="input" attribute in the struts.xml file. For example this excerpt from the struts.xml file:
<action name="Register" class="edu.ku.si.action.Register">
<result>/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action>
will cause the register.jsp view to be rendered if any of the form fields fail validation. The message text values of the field-validator nodes where validation failed will be displayed on the web page.
Styling the Validation Message
The error message(s) displayed on the web page are given a CSS class named "errorMessage." In my example project in register.jsp, I just added a page style for this class so that the message will appear in red, bold, and italics.
Summary
Using the Struts 2 validation framework can be a more flexible and cleaner way to integrate validation into a Struts 2 web application. There is much more you can do with the Struts 2 validation framework, including creating and using your own custom validators. If you need to validate the user's input and there isn't a built-in validator that does the validation you need, you can write your own custom validator. Also you can separate out the error message into a properties file and use the key attribute to specify which message to display. This technique is similar to what I did in the Struts 2 Key example. Check out the email field node in Register-validation.xml for an example of using the key attribute.
There are no comments for this entry.
[Add Comment]