![]() |
Struts 2 allows us to design our validation logic, commonly known as custom validation, by implementing the action class’s Validatable Interface. As you might expect, understanding the Validatable interface is critical for understanding Struts2 custom validation. Workflow InterceptorThe workflow interceptor verifies whether or not there are validation errors. It carries out no validation. When an action class implements the Validatable interface, it is used. For this interceptor, the default argument that decides the action or field error result to be executed is the input. We do not need to specify it directly because it is already in the default stack. Here is an example of a Workflow Interceptor: XML
Struts 2 Custom Validation methods
Steps to perform Custom ValidationStep 1: Make an input index.jspUsing struts UI tags, this jsp website generates a form. The user provides it with their name, password, and email address. <%@ taglib uri="/struts-tags" prefix="s" %> Step 2: Create the action classThis action class inherits the ActionSupport class and overrides the validate method to define the validation logic. Java
Step 3: Define a struts.xml input resultThis xml file defines an extra result by the name input, that will be called if any error message is discovered in the action class. XML
Step 4: Make a view componentIt’s a straightforward JSP file that shows the user’s data. <%@ taglib uri="/struts-tags" prefix="s" %> Output: Custom Validation FieldsOutput: FieldError Validation
|
Reffered: https://www.geeksforgeeks.org
Advance Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |