要手动建立一个validation.xml文件,和validator-rules.xml放在同一目录里面
下面是个例子
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >
<form-validation>
<formset>
<form name="loginForm"> 需要验证的Form
<field property="username" depends="required" /> Form中的字段
<field property="pwd" depends="required" />
<field property="code" depends="required"/> </form>
</formset>
</form-validation>
下面是个例子
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >
<form-validation>
<formset>
<form name="loginForm"> 需要验证的Form
<field property="username" depends="required" /> Form中的字段
<field property="pwd" depends="required" />
<field property="code" depends="required"/> </form>
</formset>
</form-validation>
第一,创建validation.xml 在<form-validation>中间增加你需要验证的表单。
<formset>
<form name="testForm">
<field property="stuNo" depends="required,mask">
<arg key="user.name"/> <!--参数 在资源文件中使用-->
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
第二、修改jsp文件 需要在form属性里增加onsubmit="return validateTestForm(this)"另增加代码<html:javascript formName="testForm"/><html:form action="/test" method="post" onsubmit="return validateTestForm(this)">
stuNo : <html:text property="stuNo"/><html:errors property="stuNo"/><br/>
stuName : <html:text property="stuName"/><html:errors property="stuName"/><br/>
<html:submit/><html:cancel/>
</html:form>
<html:javascript formName="testForm"/> 第三 修改资源文件ApplicationResouce.properties
参考第一条,在里面增加几项。 要记得转换成ascii
user.name=\u7528\u6237\u540d
errors.required={0}\u4e3a\u5fc5\u586b
errors.invalid={0}\u5fc5\u987b\u4e3a\u5b57\u6bcd\u554aPS: user.name和前面的 <arg key="user.name"/>是相对应的。因为{0}是参数。而{0}的取值是什么,决定<arg key="user.name"/>里的KEY值。找到资源文件KEY值相对应的字符串,以替换{0}
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator- rules.xml,/WEB-INF/validation.xml"/>
</plug-in>