TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1,支持自动校验的ValidatorForm:
ValidateTestForm.java
private int age;
private String name;
private String email;
2,Validate规则配置:
validation.xml
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
<formset>
<form name="validatorTestForm">
<field property="name" depends="required"><!-- 姓名非空 -->
<msg name="required" key="validatorTest.error.nameRequired"/>
</field>
<field property="age" depends="required,integer,intRange">
<!-- 验证年龄 -->
<arg0 name="intRange" key="${var:min}" resource="false"/>
<arg1 name="intRange" key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>0</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>100</var-value>
</var>
<msg name="required" key="validatorTest.error.ageRequired"/><!-- 非空 -->
<msg name="integer" key="validatorTest.error.ageInteger"/><!-- 整数 -->
<msg name="intRange" key="validatorTest.error.ageRange"/>
</field>
<field property="email" depends="required,email"><!-- 电子邮件 -->
<arg0 key="email"/>
<msg name="required" key="validatorTest.error.emailRequired"/>
<msg name="email" key="validatorTest.error.emailFormat"/>
</field>
</form>
</formset>
</form-validation>
3,错误信息配置(已编码):
validatorTest.error.ageInteger = \u5E74\u9F84\u5FC5\u987B\u4E3A\u6574\u6570
validatorTest.error.ageRange = \u5E74\u9F84\u5FC5\u987B\u4ECE{0}\u5230{1}\u4E4B\u95F4
validatorTest.error.ageRequired = \u8BF7\u8F93\u5165\u5E74\u9F84
validatorTest.error.emailFormat = \u7535\u5B50\u90AE\u4EF6\u4E0D\u5408\u6CD5
validatorTest.error.emailRequired = \u8BF7\u8F93\u5165\u7535\u5B50\u90AE\u4EF6
validatorTest.error.nameRequired = \u8BF7\u8F93\u5165\u59D3\u540D
4,配置到struts-config.xml中:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"><!-- 检验器插件 -->
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
<set-property property="stopOnFirstError" value="false"/><!-- 如有错,是否继续校验 -->
</plug-in>
5,配置客户端JavaScript校验:
validateTest.jsp
<HTML:form action="/validateTest"><!-- 校验 -->
姓名 : <html:text property="name"/><html:errors property="name"/><br/>
年龄 : <html:text property="age"/><html:errors property="age"/><br/>
电子邮件 : <html:text property="email"/><html:errors property="email"/><br/>
<html:submit value="提交"/>
<html:javascript formName="validatorTestForm"/>
</html:form>
|
|