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 configuration changes are required to use resource files in Struts?

573


Difference between struts and spring? Or why use spring, if you are already using struts?

536


What applications use apache struts?

499


What are Struts2 core components?

566


What is the purpose of @results?

586






What should be the name of xml file used for validation in struts?

543


How to build struts application in eclipse?

529


How does one create an action in struts 2?

530


How is a lookup dispatch action created?

536


What are the steps of struts installation?

574


What is the purpose of @key annotation annotation?

602


What is actioncontext?

553


How we can configured action mapping in struts?

583


What are the differences between http direct and http indirect?

472


What is actioninvocation?

544