How to create validations?what are struts validation
components?

Answer Posted / tarun

The validation in struts is done by 2 ways. one is
programmatic validation and Declarative validation. I
programmatic validation we override the
validate(ActionMapping am, HttpServletRequest req) method
provided by ActionClass. This method returns the ActionErros.
For Example :

public ActionErrors validate(ActionMapping am,
HttpServletRequest req){
ActionErrors ae = new ActionErrors();
if(username==null ||
username.equals("")){ae.add("unameError", new
ActionMessage("uname.empty"));}

if(password==null || password.equals("")){
ae.add("passError", new ActionMessage("pass.empty"));
}else if(password.length()<5){
ae.add("passError", new ActionMessage("pass.minlength"));
}
return ae;
}
The values of pass.minlength , pass.empty, uname.empty will
be described in the properities file.

And in the case of declarative validation we extend the form
bean class from anyone of these classes DynaValidatorForm,
DynaValidatorActionForm

the first two classes are defined in
org.apache.struts.validator.*;
In this case we need validator.xml and validator-rules.xml
and properties file.

Is This Answer Correct ?    17 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is controller in struts2?

694


What are the struts2 error message keys that can come during file uploading process?

804


What is the purpose of @customvalidator annotation?

738


What is the use of resourcebundle.properties file in Struts Validation framework?

802


What is the default suffix for struts2 action uri ?

721


Are the struts tags xhtml compliant ?

713


What are the different kinds of actions in struts?

788


Explain about struts dispatch action?

749


What are the features of struts?

771


Explain struts?

759


Mark the differences between html tags and strut specific html tags.

768


What is spring and struts in java?

707


What do you mean by actionform?

741


What is a custom tag?

707


How can we get Servlet API Request, Response, HttpSession etc Objects in action classes?

815