Wednesday, January 16, 2013

Struts Java Validation

                                           JAVA STRUTS VALIDATION
We have created an example struts java project and routed previously. Now lets control it if there is error or have the value that we do not want.
We will see
How to control automatically generated form bean?
How to route from control mechanism?

Form bean that is extended from ActionForm has a function named as validate which you can see if you click right while your cursor is in the form class then  open source and Override/Implement methods. Choose validate as in below.
This method will give ActionMapping and request object so you can use mapping.findForward(...) to route or you can use another mechanism that is if you return errors from this function that is in the type of ActionErrors, the struts mechanism will route it to the input tag. for example
<action path="/comingFormPath" name="ActionFormName" validate="true" input="MyErrorFile.jsp"
type="actionclass">

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
ActionErrors errors= new ActionErrors();
//if there is error 
ActionMessage message = new ActionMessage("tag.in.properties.file");
actionErors.add("yourTag",message);
return errors;
}
So; if errors filled by add method then the struts structure will route it to the input file that the yourTag is your propertyNames will reflect the properties equalities that are attended with add method.

No comments:

Post a Comment