.在validation.xml中进行相关的验证配置如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "validator_1_0.dtd" >
<form-validation>
<formset>
<form name="loginForm">//struts-config.xml中formBean的名字
<field property="username" depends="required,maxlength,minlength">
//property相关验证字段的名称,depends所对应的验证器
<arg0 key="用户名" resource="false"/>
//当resource为TRUE时,表示使用来自resource Bundle中的消息,反之指定key中消息
<arg1 name="maxlength" resource="false" key="${var:maxlength}"/>
<var>
<var-name>maxlength</var-name>
<var-value>8</var-value>
</var>
<arg2 name="minlength" resource="false" key="${var:minlength}"/>
<var>
<var-name>minlength</var-name>
<var-value>2</var-value>
</var>
</field>
</form>
</formset>
所注意的问题:1、如<arg0 key="用户名" resource="false"/>在arg0中如果不设置验证器,那么就被所有的验证器通用
2、如<arg1 name="maxlength" resource="false" key="${var:maxlength}"/>中的arg1要与错误信息中所对应的该验证器的参数一至(errors.maxlength={0} can not be greater than {1} characters.)
如果(errors.maxlength={0} can not be greater than {2} characters.)的话,那么就必须将2改为1
7.jsp中的前台验证:
<htm:form arction =”” onsubmit=” return validateLoginForm(this) ”>
<html:javascript formName="loginForm"/>
//对应formBean的名字