在Struts使用Validate的步骤
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 ------------------------
...
publicActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest){
ActionErrorserrors=newActionErrors();
//Validateanattributenamed"xxx"
if(getUsername()==null||getUsername().length()<3){
errors.add("name",newActionMessage("error.username.required",username));
}
if(getPassword()==null||getPassword().length()<3){
errors.add("pwd",newActionMessage("error.password.required",password));
}
returnerrors;
}
...
----------------------------------------------------
2.
---------------------------------------------------- ApplicationResources.properties ---------------------------------
#Usernameerror
error.username.required=<li>Pleaseinputyourusernameagain!</li>
#Passworderror
error.password.required=<li>Pleaseinputyourpasswordagain!</li>
----------------------------------------------------
3.
---------------------------------------------------- struts-config.xml -----------------
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!--
ThisisablankStrutsconfigurationfilewithanexample
welcomeaction/pageandothercommentedsampleelements.
TilesandtheStrutsValidatorareconfiguredusingthefactorydefaults
andareready-to-use.
NOTE:IfyouhaveageneratortooltocreatethecorrespondingJavaclasses
foryou,youcouldincludethedetailsinthe"form-bean"declarations.
Otherwise,youwouldonlydefinethe"form-bean"elementitself,withthe
corresponding"name"and"type"attributes,asshownhere.
-->
<struts-config>
<form-beans>
<form-beanname="loginActionForm"type="login.loginActionForm"/>
</form-beans>
<global-exceptions/>
<global-forwards>
<forwardname="welcome"path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<actionforward="/pages/Welcome.jsp"path="/Welcome"/>
<actioninput="/login/login.jsp"name="loginActionForm"path="/loginAction"scope="request"type="login.loginAction"validate="true">
<forwardname="Success"path="/login/main.jsp"/>
<forwardname="Fail"path="/login/register.jsp"/>
</action>
</action-mappings>
<controllerprocessorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<plug-inclassName="org.apache.struts.tiles.TilesPlugin">
<set-propertyproperty="definitions-config"value="/WEB-INF/tiles-defs.xml"/>
<set-propertyproperty="moduleAware"value="true"/>
</plug-in>
<plug-inclassName="org.apache.struts.validator.ValidatorPlugIn">
<set-propertyproperty="pathnames"value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
<message-resourcesparameter="ApplicationResources"></message-resources>
</struts-config>
----------------------------------------------------
4.
---------------------------------------------------- login.jsp ----------












----------------------------------------------------