Struts 2 validation is configured via XML or annotations. Manual validation in the action is also possible, and may be combined with XML and annotation-driven validation, see more about Struts 2 Validation.
In this example we will see one simple user name manual validation in action class.
1. Create a web project.
Create a dynamic web project and all struts 2 related jars in to lib folder. Here is the final structure of this example.
2. Create an Action class (UserAction.java)
The validate method is taking care validating and throwing error messages.
3. Create a JSP page (index.jsp)
This jsp file contains input field for user name and submit button
4. Create a success page
7. Test Validation
Run this application and click on submit button without any username , we will see below validation error messages
Download this example: Struts 2 Simple Validation