Struts 2 Custom Validation - Workflow Interceptor

We can characterize our own approval rationale (custom approval) in struts 2 by executing the Validateable interface in the activity class.

The workflow interceptor is utilized to get data about the mistake messages characterized in the activity class.

Workflow Interceptor

The workflow interceptor checks if there is any approval mistakes or not. It doesn't play out any approval.

It is connected when activity class actualizes the Validateable interface. The input is the default parameter for this interceptor that decides the outcome to be summoned for the activity or field error.

It is found in the defaultStack so we don't have to characterize it explicitly.

Parameters of work process interceptor

There is just 1 parameter characterized for work process interceptor.

Parameters of work process interceptor

There is just 1 parameter characterized for work process interceptor.

inputResultName indicates the outcome name to be returned whether field mistake or activity blunder is found. It is set to input bydefault.

Validateabale interface

The Validateable interface must be executed to perform approval rationale in the activity class. It contains just a single technique validate() that must be superseded in the activity class to characterize the approval rationale. Mark of the approve technique is:

  1. public void validate();  

ValidationAware interface

The ValidationAware interface can acknowledge the field level or action class level mistake messages. The field level messages are kept in Map and Action class level messages are kept in gathering. It ought to be actualized by the activity class to include any mistake message.

Methods of ValidatationAware interface

The methods of ValidationAware interface are as follows:

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.

Steps to perform custom validation

The steps are as follows:

  1. create the structure to get contribution from the user
  2. Define the approval rationale in real life class by broadening the ActionSupport class and superseding the approve method
  3. Define result for the mistake message by the name contribution to struts.xml file

Example to perform custom validation

In this model, we are making 4 pages :

  1. index.jsp for contribution from the user.
  2. for characterizing the approval logic.
  3. struts.xml for characterizing the outcome and action.
  4. welcome.jsp for the view component.

1) Create index.jsp for info

This jsp page makes a structure utilizing struts UI labels. It gets name, secret phrase and email id from the client.

  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2. <s:form action="register">  
  3. <s:textfield name="name" label="Name"></s:textfield>  
  4. <s:password name="password" label="Password"></s:password>  
  5. <s:submit value="register"></s:submit>  
  6. </s:form>  

2) Create the activity class

This activity class acquires the ActionSupport class and abrogates the approve strategy to characterize the approval logic.
  1. package com.Javacodegeeks;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  4. public class RegisterAction extends ActionSupport{  
  5. private String name,password;  
  6. public void validate() {  
  7.     if(name.length()<1)  
  8.         addFieldError("name","Name can't be blank");  
  9.     if(password.length()<6)  
  10.         addFieldError("password","Password must be greater than 5");  
  11. }  
  13. //getters and setters  
  15. public String execute(){  
  16. //perform business logic here  
  17.     return "success";  
  18. }  
  19. }  

3) Define an info result in struts.xml

This xml document characterizes an additional outcome by the name input, that will be conjured if any mistake message is found in the activity class.

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "- //Apache Software Foundation//DTD Struts  
  3.  Configuration 2.1//EN" " 2.1.dtd">  
  4. <struts>  
  6. <package name="default" extends="struts-default">  
  7. <action name="register" class="com.Javacodegeeks.RegisterAction">  
  8. <result>welcome.jsp</result>  
  9. <result name="input">index.jsp</result>  
  10. </action>  
  11. </package>  
  12. </struts>      

4) Create see component

It is the straightforward jsp document showing the data of the user.

  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2. Name:<s:property value="name"/><br/>  
  3. Password:<s:property value="password"/><br/>  
struts 2 custom validation example output 1

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition