aspose file tools*
The moose likes Struts and the fly likes server side struts validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "server side struts validation " Watch "server side struts validation " New topic
Author

server side struts validation

smitha mano
Ranch Hand

Joined: Sep 26, 2005
Posts: 70
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

Joined: Jan 23, 2003
Posts: 78
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

Joined: Sep 26, 2005
Posts: 70
no, i need in dispatchaction class
smitha mano
Ranch Hand

Joined: Sep 26, 2005
Posts: 70
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

Joined: Jan 23, 2003
Posts: 78
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

Joined: Apr 14, 2005
Posts: 354
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.


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: server side struts validation