In this tutorial we'll explore using Struts 2 to validate the user's input on a form. There are two ways you can use Struts 2 to do form validation. This tutorial will cover the more basic method, where the validation is included in the Struts 2 Action class.
To vaildate a form simple override a default vailidate() method from ActionSupport class
Code
public void validate() {
if (getUname().length() == 0) {
addFieldError("uname", "User Name is required");
}
if (getPass().length() == 0) {
addFieldError("pass", "password is required");
}
else if (!(getUname().equals("candidjava") & getPass().equals("1234"))) {
this.addActionError("Invalid User Name or Password");
}
}
Screenshot
Download
Struts 2 form validation maven zip