Steps to using validate() in ActionForm:
1.rewrite the method validate() in ActionForm file;
2.in resource file ApplicationResources.properties create error marks;
3.in struts-config.xml, set validate property of <action> to TRUE, and set <message-resources> element to indicate where the resource file locates;
4.in related jsp files, add <html:errors>.
Example:
1.
----------------------------------------------------
LoginActionForm.java
------------------------
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate an attribute named "xxx"
if ( getUsername() == null || getUsername().length() < 3 ){
errors.add( " name " , new ActionMessage( " error.username.required " ,username));
}
if ( getPassword() == null || getPassword().length() < 3 ){
errors.add( " pwd " , new ActionMessage( " error.password.required " ,password));
}
return errors;
}
...
----------------------------------------------------
2.
----------------------------------------------------
ApplicationResources.properties
---------------------------------
error.username.required = <li>Please input your username again!</li>
#Password error
error.password.required = <li>Please input your password again!</li>
----------------------------------------------------
3.
----------------------------------------------------
struts-config.xml
-----------------
<!--
This is a blank Struts configuration file with an example
welcome action/page and other commented sample elements.
Tiles and the Struts Validator are configured using the factory defaults
and are ready-to-use.
NOTE: If you have a generator tool to create the corresponding Java classes
for you, you could include the details in the "form-bean" declarations.
Otherwise, you would only define the "form-bean" element itself, with the
corresponding "name" and "type" attributes, as shown here.
-->
< struts-config >
< form-beans >
< form-bean name ="loginActionForm" type ="login.loginActionForm" />
</ form-beans >
< global-exceptions />
< global-forwards >
< forward name ="welcome" path ="/Welcome.do" />
</ global-forwards >
< action-mappings >
< action forward ="/pages/Welcome.jsp" path ="/Welcome" />
< action input ="/login/login.jsp" name ="loginActionForm" path ="/loginAction" scope ="request" type ="login.loginAction" validate ="true" >
< forward name ="Success" path ="/login/main.jsp" />
< forward name ="Fail" path ="/login/register.jsp" />
</ action >
</ action-mappings >
< controller processorClass ="org.apache.struts.tiles.TilesRequestProcessor" />
< plug-in className ="org.apache.struts.tiles.TilesPlugin" >
< set-property property ="definitions-config" value ="/WEB-INF/tiles-defs.xml" />
< set-property property ="moduleAware" value ="true" />
</ plug-in >
< plug-in className ="org.apache.struts.validator.ValidatorPlugIn" >
< set-property property ="pathnames" value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</ plug-in >
< message-resources parameter ="ApplicationResources" ></ message-resources >
</ struts-config >
----------------------------------------------------
4.
----------------------------------------------------
login.jsp
----------
html:form action="loginAction" method="post">
Username: < html:text property ="username" size ="15" />
< font color ="red" >< html:errors property ="name" /></ font >
< br />
< br />
Password: < html:password property ="password" size ="15" />
< font color ="red" >< html:errors property ="pwd" /></ font >
< br />
< br />
< html:submit property ="submit" value ="Login" />
</ html:form >
...
----------------------------------------------------