对表单的验证有两种:语法验证和语义验证。语法有效是指数据必须有正确的格式,如数据只能包含数字,语义有效是指必须符合逻辑,如时间值不能是一个未来的时间。
struts1.2
增添了许多新特征如:
validwhen
验证规则
(validator)
一开始
Struts
是使用
formbean
的
validate()
方法进行验证的。有时候则是在
Action
中进行验证。通过
validator
框架你可以不需要通过硬编码的方式进行验证,它使你可以在一个
XML
中配置对表单的验证,这样做可以很灵活的添加修改删除验证,同时可以重复使用验证规则。你可以使用它在客户端进行基于
javascript
和服务器端的验证,你也可以自定义验证方法。
具体的验证规则如下:
required
|
Checks that a field value is non-null and not an empty string.
|
requiredif
|
Only available for Struts 1.1. This validator allows one field to be specified as required if another field is null, not null, or equal to a specified value. This validator is deprecated, in favor of validwhen, in releases after Struts 1.1.
|
validwhen
|
Designed to replace requiredif, this validator is available in releases after Struts 1.1. This validator relies on a user-specified test expression that can include references to other fields, field values, and logical relationships.
|
minlength
|
Checks that the number of characters in the field value is greater than or equal to a specified minimum.
|
maxlength
|
Checks that the number of characters in the field value is less than or equal to a specified maximum.
|
mask
|
Validates the field value using a regular expression to determine a match. If the value matches the regular expression, the field is valid.
|
byte
|
Checks that the field value is a valid byte value.
|
short
|
Checks that the field value is a valid short integer value.
|
integer
|
Checks that the field value is a valid integer value.
|
long
|
Checks that the field value is a valid long value.
|
float
|
Checks that the field value is a valid floating-point value.
|
double
|
Checks that the field value is a valid double value.
|
date
|
Checks that the field value matches a specified date format pattern (e.g., MM/dd/yyyy). The match can be strict or lenient; "strict" would require May 10, 1963 to be formatted (using the MM/dd/yyyy pattern) as 05/10/1963; "lenient" would allow 5/10/1963.
|
range
|
Checks that the field value is within a specified numeric range. This valdiator has been deprecated in favor of the type-specific range checks (intRange, floatRange, etc.).
|
intRange
|
Checks that the field value is within a range bounded by two int values.
|
floatRange
|
Checks that the field value is within a range bounded by two float values.
|
doubleRange
|
Checks that the field value is within a range bounded by two double values.
|
creditCard
|
Verifies that the format of the field value is valid for a credit card number. This validator is convenient to use instead of using mask.
|
email
|
Verifies that the format of the field value is valid for an electronic mail address (e.g., foo@bar.com). This validator is convenient to use instead of working with mask.
|
url
|
Verifies that the value entered is a valid uniform resource locator. Use this validator when you want to validate an input web location or hyperlink value.
|
要使用
Validator
验证框架,首先必须在
struts-config.xml
中指定
plug-in
元素。
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
|
pathnames
指定你要使用的
Validator
配置文件。可以设置一个或多个。第一个
validator-rules.xml
定义
各个验证规则
,
第二个
validation.xml
定义了验证框架如何应用到你的应用程序中去。
一个好的方法是为应用程序的每个功能模块定义一个
validation.xml
。一个大型的应用程序可能有一大堆这样的文件
要使用验证框架的
form bean
,如果是硬编码的
formbean
则必须继承
org.apache.struts.action.ValidatorForm
。如果是动态
formbean
则必须是
org.apache.struts.action.DynaValidatorForm
或它的字类。
下面一个实例验证
MyLoginForm
表单的
username
和
password
字段是否为空,并限制
password
的最大值和最小值
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="MyLoginForm">
<field property="username"
depends="required">
<arg key="prompt.username"/>
</field>
<field property="password"
depends="required, minlength,maxlength">
<arg key="prompt.password"/>
<arg key="${var:minlength}" name="minlength"
resource="false"/>
<arg key="${var:maxlength}" name="maxlength"
resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>16</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
</field>
</form>
<formset>
<form-validation>
form
元素中的
name
属性指定要验证的表单名而不是类。
|
如果
formbean
的类型为
ValidatorActionForm
或
DynaValidatorActionForm
,则验证就和请求路径关联而不是
formbean
。当一个表单被提交到一个
Action
时,只有与路径对应的验证起作用。举例如下:
<form name="/ProcessStep1">
<field property="username"
depends="required">
<arg key="prompt.username"/>
</field>
</form>
<form name="/ProcessStep2>
<field property="password"
depends="required">
<arg key="prompt.password"/>
</field>
</form>
只有当
struts-config.xml
的
action
元素的
validate
属性为
true
时才进行验证(
validate
默认值为
true
)。如果验证失败则
Struts
跳转到由
action
元素的
input
属性指定的位置。
<action path="/Login"
type="com.oreilly.strutsckbk.ch08.LoginAction"
scope="request"
name="MyLoginForm"
validate="true"
input="/login.jsp">
<forward name="success" path="/login_success.jsp"/>
</action>