Struts 2 Validation
User input validations are integral part of any web application. With the release of Struts 2, validation are now much easier and robust. Struts 2 support Ajax based and even server side validations. Struts 2.0 action relies on the XWork validation framework. The XWork framework runs the validation rules before executing the action. Struts 2 also provides the facility to define validation rules in your action class using Java 5 annotations.
Struts 2 Validation (Int Validator)
Struts 2 Framework provides in-built validation functions to validate user inputs. These validation functions are sufficient for any normal web application.
2 RequiredString validator
This section discusses RequiredString validator of Struts 2 framework. RequiredStringValidator checks the String field is non-null and its length is > 0. (i.e. it isn't "").
2 double validator
The Double validator of Struts 2 Framework checks if the given input is double or not. If the input is not double, it generates the error message. Double validator can also be used to check the input range.
- Struts 2
The Date validator in the Struts 2 Framework checks whether the supplied date lies within a specific range or not. If the value supplied does not lie in the specified range, it generates an error message.
- Struts 2
The email validator in Struts 2 Framework checks whether a given String field is empty or not and contains a valid email address or not. If the entered value does not match with the email type, then the e-mail validator generates an error message.
- Struts 2
The URLValidator of Struts 2 Framework checks whether the String contained within the given field is a valid URL or not. If the entered value is not a valid URL, it generates an error message.
- Validations using Struts 2 Annotations
In this section we are going to validate our login application using Annotations in Action class. Our current login application does not validate the user against the database. Instead login name and passwords are validated against the hardcode values (User: Admin and Password: Admin) in the action class.