This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

server side struts validation

 
smitha mano
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to server side validation using struts MyAction class

please give me the steps procedures

in my application
myAction extends DispatchAction

here i used in the developMethod()
if(myForm.getName()== null || myForm.getName().equals("")){
errors.add("Name",new ActionError("error.myForm.Name"));
return mapping.findForward("failure");
}

and struts-config.xml file

<action
input="page.myFile"
validate="true"
name="myForm"
path="/myAction"
parameter="action"
scope="request"
type="com.aymex.struts.test.MyAction" />

<form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="Name" type="java.lang.String" />
 
sajjad ahmad
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
first of all i will suggest that the type of validation you are trying must be kept in ActionForm bean instead of Action as it is related to bean level validation ......the steps are
1)define vaidationplugin tag in struts config as
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> (if it isn't already present there)
2)Inside your ActionForm bean's validate method create those error messages depending on whetever condition you want and return errors from validate method (this way your you action class wont be called and controll will be transfered back to your current page )
2.a)you may also catch some "business logic" related errors in your Action class the same way you did but after making/populating erros object you need to write 2 other lines
saveErrors(servletRequest, errors);//saves the errors object for jsp page
return actionMapping.getInputForward();//returns control to current page
Also inside the relted action tag in struts config you need to write validate="true" attribute .
3) at the top of your page use following tag <html:errors/> and jsp page will show all the errors you sent to it from ActionFormBean or from Action class
hope this helps
best of luck
sajjad ahme dparacha
 
smitha mano
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, i need in dispatchaction class
 
smitha mano
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ApplicationResource.properties

error.myForm.name=Name not entered!
error.myForm.country=Country not entered!

MyAction extends DispatchAction
{

ActionErrors errors = new ActionErrors();
public ActionForward create(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
MyForm myForm= (MyForm) form;
//Check for mandatory data
if(myForm.getName()== null || myForm.getName().equals("")){
errors.add("Name",new ActionError("error.myForm.Name"));
return mapping.findForward("failure");
}
if(myForm.getCountry()== null || myForm.getCountry().equals("")){
errors.add("country",new ActionError("error.myForm.country"));
return mapping.findForward("failure");
}



jsp page

<html:form action="myAction.do" enctype="multipart/form-data">

<tr>
<td width="88"><bean:message key="myForm.Name"/></td>
<td colspan="5"><html:text property="Name" /></td>
<td><html:errors property="patientName" /></td>
</tr>
<tr>
<td><bean:message key="testimonialForm.country"/></td>
<td colspan="5"><html:select property="country" onchange="go('display');submit();">
<html ptions collection="countryall" property="name" />
</html:select></td>
<td><html:errors property="country"/></td>
</tr>

my struts.config

<action
input="page.myForm"
validate="true"
name="myForm"
path="/MyAction"
parameter="action"
scope="request"
type="com.aymex.struts.test.MyAction" >
<forward name="load" path="page.myForm" />
</action>
 
sajjad ahmad
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i haven't used that dispatchAction class before but have you explored meterial provided in struts FAQ and other jakarta sources and have you gone through struts-example application??? i found both of them the excelent resources to solvenay of those problems....also try googling this query....
 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after calling errors.add(), you must call the Action.saveErrors(request, errors) method to place the error into the request object. in your failure display page, add html:errors to show the error.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan is correct. Also, when you are explicitly calling validation logic in the Action class it is not necessary to specify validate="true" in the struts-config.

It's also customary to place syntactic validations (which includes whether a field is required) in the validate() method of the ActionForm even when the validation is explicitly called from the Action.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic