Struts 2 validation Introduction

Struts2 Validation Framework

As the security and efficiency point of view validation of form’s input is necessary for every web application, to ensure that users enter valid information. The Apache Struts2 framework provides built-in robust validation support  framework which is highly decoupled from the view and controller, thus it can be easily integrated to existing applications. In this section, we’ll learn  how Struts2 validation works by creating a sample application that validates user’s input for a simple login form. Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation.

By default, the validation framework is included in Struts2 default stack and is “turned on” already so we don’t have to do anything to use it, except creating validation rules in an XML file in a correct manner and putting it into right place. We specify validation rules per action class in an XML file that follows either of these naming conventions:

Client side validation is usually achieved using Javascript. But one should not rely upon client side validation alone. Best practise suggests that the validation should be introduced at all levels of your application framework. Now let us look at two ways of adding validation to our Struts project.