<form-bean name="PlayerForm" type="....DynaValidatorForm">
<form-property name="name" type="java.lang.String"/>
<form-property name="sex" type="java.lang.Integer"/>
<form-property name="birthdate" type="java.lang.String"/>
<form-property name="height" type="java.lang.String"/>
<form-property name="weight" type="java.lang.String"/>
<form-property name="goals" type="java.lang.String"/>
</form-bean>
以上在struts-config.xml中定义。其中我们希望name,birthdate和goals必输入,name长度不能超过40,birthdate的格式必须是日期,height和weight是浮点数,goals是整数而且必须大于0小于2000,sex在FORM中是选择项,因此可以不验证。则在validation.xml中定义如下:
<form name="PlayerForm">
<field property="name" depends="required,maxlength">
<arg0 key="label.player.name"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>40</var-value>
</var>
</field>
<field property="birthdate" depends="required,date">
<arg0 key="label.player.birthdate"/>
</field>
<field property="height" depends="float">
<arg0 key="label.player.height"/>
</field>
<field property="weight" depends="float">
<arg0 key="label.player.weight"/>
</field>
<field property="goals" depends="required,integer,intRange">
<arg0 key="label.player.goals"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 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>2000</var-value>
</var>
</field>
</form>
注意,birthdate、height、weight、goals等值是字符串型,在Action类Class中需转换成各自目标的类型,而sex则不用转换。
No comments:
Post a Comment