Javatpoint Logo
Javatpoint Logo

Struts 2 Custom Validation - Workflow Interceptor

We can define our own validation logic (custom validation) in struts 2 by implementing the Validateable interface in the action class.

The workflow interceptor is used to get information about the error messages defined in the action class.

Workflow Interceptor

The workflow interceptor checks if there is any validation errors or not. It doesn't perform any validation.

It is applied when action class implements the Validateable interface. The input is the default parameter for this interceptor that determines the result to be invoked for the action or field error.

It is found in the defaultStack so we don't need to define it explicitly.

Parameters of workflow interceptor

There is only 1 parameter defined for workflow interceptor.

ParameterDescription
inputResultName specifies the result name to be returned if field error or action error is found. It is set to input bydefault.

Validateabale interface

The Validateable interface must be implemented to perform validation logic in the action class. It contains only one method validate() that must be overridden in the action class to define the validation logic. Signature of the validate method is:


ValidationAware interface

The ValidationAware interface can accept the field level or action class level error messages. The field level messages are kept in Map and Action class level messages are kept in collection. It should be implemented by the action class to add any error message.

Methods of ValidatationAware interface

The methods of ValidationAware interface are as follows:

MethodDescription
void addFieldError(String fieldName,String errorMessage) adds the error message for the specified field.
void addActionError(String errorMessage) adds an Action-level error message for this action.
void addActionMessage(String message)adds an Action-level message for this action.
void setFieldErrors(Map<String,List<String>> map) sets a collection of error messages for fields.
void setActionErrors(Collection<String> errorMessages)sets a collection of error messages for this action.
void setActionMessages(Collection<String> messages)sets a collection of messages for this action.
boolean hasErrors() checks if there are any field or action errors.
boolean hasFieldErrors() checks if there are any field errors.
boolean hasActionErrors() checks if there are any Action-level error messages.
boolean hasActionMessages()checks if there are any Action-level messages.
Map<String,List<String>> getFieldErrors() returns all the field level error messages.
Collection<String> getActionErrors() returns all the Action-level error messages.
Collection<String> getActionMessages() returns all the Action-level messages.

Note: ActionSupport class implements Validateable and ValidationAware interfaces, so we can inherit the ActionSupport class to define the validation logic and error messages.


Steps to perform custom validation

The steps are as follows:

  1. create the form to get input from the user
  2. Define the validation logic in action class by extending the ActionSupport class and overriding the validate method
  3. Define result for the error message by the name input in struts.xml file

Example to perform custom validation

In this example, we are creating 4 pages :

  1. index.jsp for input from the user.
  2. RegisterAction.java for defining the validation logic.
  3. struts.xml for defining the result and action.
  4. welcome.jsp for the view component.

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.

index.jsp

2) Create the action class

This action class inherits the ActionSupport class and overrides the validate method to define the validation logic.

RegisterAction.java

3) Define a input result in struts.xml

This xml file defines an extra result by the name input, that will be invoked if any error message is found in the action class.

struts.xml

4) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp

Output

struts 2 custom validation example output 1 struts 2 custom validation example output 2

Defining action level error message

The action level error message works for the whole form. We can define the action level error message by addActionError() method of ValidationAware interface in validate() method. For example:

Now you need to use actionerror tag in index.jsp file to display the action level error message.

index.jsp

Output

struts 2 custom validation example output 1 struts 2 custom validation example output 4




Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA