一 form表单必须要实现继承validateForm
并且不能实现validate方法
不然会默认使用validate方法验证
package comjinchunstrutsform;
import javaxservlethttpHttpServletRequest;
import orgapachestrutsactionActionErrors;
import orgapachestrutsactionActionMapping;
import orgapachestrutsvalidatorValidatorForm;
public class LoginForm extends ValidatorForm {
private String sex;
public void reset(ActionMapping mapping HttpServletRequest request) {
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
thissex = sex;
}
}
}
二
使用validator_rulesxml新建validationxml文件作为验证规则文件
<!DOCTYPE formvalidation PUBLIC
//Apache Software Foundation//DTD Commons Validator Rules Configuration //EN
___dtd>
<formvalidation>
<formset>//里面可以验证多个表单
<form name=loginForm>//验证loginForm表单这个表单名称和//nfigxml文件中的表单名称相对应
<field property=uname depends=requiredmask>//验证必填和规则两项
<arg key=namelab/>
<var>
<varname>mask</varname>
<varvalue>[AZaz]</varvalue>
</var>
</field>
<field property=age depends=requiredintRange>//验证必填和范围项
<arg key=agelab/>
<arg name=intRange key=${var:min} resource=false/>
<arg name=intRange key=${var:max} resource=false/>
<var>
<varname>min</varname>
<varvalue></varvalue>
</var>
<var>
<varname>max</varname>
<varvalue></varvalue>
</var>
</field>
</form>
</formset>
</formvalidation>
三 资源文件里面如下
ApplicationResourcesproperties
# Resources for parameter comjinchunstrutsApplicationResources
# Project struts_validate
namelab=姓名
errorsrequired=<font size= color=red>{}是必须填的</font>//对应必填
errorsrange=<font size= color=red>{}是必须在{}和{}之间的</font>//对//应范围
agelab=年龄
errorsinvalid=<font size= color=red>{}是无效的</font>//对应mask
sexlab=性别
tellab=电话
emaillabe=email地址
四 strutsconfigxml文件中
<messageresources
parameter=comjinchunstrutsApplicationResources />
<plugin className=orgapachestrutsvalidatorValidatorPlugIn>
<setproperty property=pathnames
value=/WEBINF/validatorrulesxml
/WEBINF/validationxml />
</plugin>//这句话是从validatorrulesxml中复制的
</strutsconfig>