Limited Period Offer - Upto 50% OFF | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Validations in struts
Validations in strutsValidation

To keep away from the wrong esteems, we have to perform approval on frames where client presents a few esteems. For instance, if client composes his/her email id as abc, we have to give blunder message to the client that the given email id is not right With the goal that we can have just significant information.

There are three approaches to perform approval in struts 2.1) By Custom Validation Here, we should execute the Validateable interface (or expand ActionSupport class) and give the usage of approve technique.2) By Input Validation (worked in validators) Struts 2 gives a considerable measure of predefined that can be utilized as a part of struts 2 application to perform approval.3)By Ajax Validationwe can use jsonValidation interceptor to perform validation with ajax.

Validateabale interface

The Validateable interface must be actualized to perform approval rationale in the activity class. It contains just a single strategy approve() that must be abrogated in the activity class to characterize the approval rationale. Mark of the approve technique .

public void validate();validationAware interface

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

 Methods of ValidatationAware interface
Method
void addFieldError(String fieldName,String errorMessage)
void addActionError(String errorMessage)
void addActionMessage(String message)
void setFieldErrors(Map> map)
void setActionErrors(Collection errorMessages)
void setActionMessages(Collection messages)
boolean hasErrors()
boolean hasFieldErrors()
boolean hasActionErrors()
boolean hasActionMessages()
Map> getFieldErrors()
Collection getActionErrors()
Collection getActionMessages()
 Validations in strutsBundled Validators

Struts 2 approval system gives many inherent validators otherwise called packaged validators for email, string, int, twofold, url, date and so forth In this way, we don't have to give express rationale to email, twofold, url and so forth. For giving particular approval rationale, we can utilize regex, which we will see later.

validation interceptor

It performs approval against the predetermined approval standards and includes the field-level and activity level mistake messages. It works in conjunction with the work process interceptor to show the mistake messages. There is no parameter characterized for this interceptor.

Favorable position of Bundled validatorsQuick improvement since we don't have to determine the normal validators, for example, email date, string length and so forth.Bundled validatorsTwo ways to use bundled validators
  1. Plain-Validator (non-field validator) Syntax
  2. Field-Validator Syntax
Plain-Validator (non-field validator) Syntax type="requiredstring"> name="fieldName">username name="trim">trueusername is requiredField-Validator Syntax name="username"> type="requiredstring"> name="trim">trueusername is required requiredstring validation The requiredstring validator determines that string can't be invalid or clear. It trims the string bydefault at that point checks if its length is more prominent that 0.Parameters of requiredstring validator
Parameter
fieldName
trim
 example of requiredstring validatorCreate index.jsp for inputindex.jsp<%@ taglib uri="/struts-tags" prefix="s" %> type="text/css">.errorMessage{color:red;} action="register"> name="username" label="Username"> name="userpass" label="Password"> value="register">2) Create the action class RegisterAction.javapackage com.javaspot;import com.opensymphony.xwork2.ActionSupport;public class Register extends ActionSupport{private String username,userpass;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpass() {return userpass;}  public void setUserpass(String userpass) {this.userpass = userpass;}public String execute(){return "success";}}3) Create the validation file Register-validation.xml version="1.0" encoding="UTF-8"?>> name="username"> type="requiredstring">Name can't be blank4) Create struts.xml struts.xml version="1.0" encoding="UTF-8" ?>> name="default" extends="struts-default"> name="register" class="com.javaspot.Register"> name="input">index.jspwelcome.jsp5) Create view componentIt is the simple jsp file displaying the information of the user.welcome.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Welcome, value="username"/>String Length Validation

The stringlength validator indicates that string must be of given length. It can be utilized as a part of username, secret key and so on. It trims the string by default at that point checks if its length is of the given length.

 Parameters of stringlength validator
Parameter
fieldName
minLength
maxLength
trim
Email Validation The email validator watches that given field has substantial email address. It works just if field is not clear.Parameters of email validator
ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.
Example of email validator type="email"> name="fieldName">emailPlease enter a valid email address  name="email"> type="email">Please enter a valid email address. Date Validation:It will checks the date is in the specified range or not.
ParameterDescription
fieldNameIt is validated. And required in Plain-Validator only.
minminimum range.
max maximum range
 1) Create index.jsp for input index.jsp
  1. <%@ taglib uri="/struts-tags"prefix="s" %>
2) Create the action class RegisterAction.javapackage com.javaspot;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class Register extends ActionSupport{private Date dob;public Date getDob() {return dob;}public void setDob(Date dob) {this.dob = dob;}public String execute(){return "success";}}3) Create the validation fileRegister-validation.xml01/01/195001/01/2010Date of Birth must be between ${min} to ${max} 4) Create struts.xml struts.xml<package name="default" extends="struts-default">class
="com.javaspot.Register">index.jspwelcome.jsppackage
>5) Create view component welcome.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Date of Birth:int validationThe productId, employeeId can be used by int validator it can check given number is in specified range. Parameters of int validator 
Parameter
fieldName
min
max
 double validationProduct price is used in double validation, it is in floating point of specified range. Parameters of double validator 
Parameter
fieldName
minInclusive
maxInclusive
minExclusive
maxExclusive
Example: type="double"> name="fieldName">price name="minInclusive">100.0 name="maxInclusive">10000.0Price must be between ${minInclusive} and ${maxInclusive} name="price"> type="double"> name="minInclusive">100.0 name="maxInclusive">10000.0Price must be between ${minInclusive} and ${maxInclusive}url validation It is used in website url ,reciprocal links, it  checks the value is in string with valid url.Parameters of url validator
ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.
 Example of url validator  type="url"> name="fieldName">websiteInvalid website url type="url"> name="website">Invalid website urlRegex ValidationPassword and security key can be used in Regex Validation,the given string with regular expression.Parameters of regex validator
Parameter
fieldName
expression
caseSensitive
trim
 Example of regex validator  type="regex"> name="fieldName">data name="expression">[A-Z,a-z,0-9]{5}data must be alpha numeric of 5 digits name="data"> type="regex"> name="expression">[A-Z,a-z,0-9]{5}data must be alpha numeric of 5 digits