WebWork2 的 Validation

在WebWork2包的文档中有一段关于验证器的内容,并举出了一些例子,但这些例子可能无法满当前的要求,于是我在文档中找到这样一段内容:
Please consult XWork's validation framework documentation for complete details
这个地址连接到http://wiki.opensymphony.com/display/XW/Standard+Validators
,这里描述了一些复杂验证器的应用方法。其中我看到了以下内容:
(注:粗黑体字是要验证的表单控件名称)
-----------------------------------------------------------------------------

第一种方法:

ExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the action if the expression returns false when it is evaluated against the value stack.
Parameter  Required  Default  Notes 
expression  yes   An OGNL expression that returns a boolean value. 

Example:
<validator type="expression">
    <param name="expression"> foo > bar</param>
    <message default="Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}."/>
</validator>
-------------------------------------------------------------------------------

第二种方法:

FieldExpressionValidator

This validator uses an OGNL expression to perform its validation. The error message will be added to the field if the expression returns false when it is evaluated against the value stack.
Parameter  Required  Default  Notes 
expression  yes   An OGNL expression that returns a boolean value. 

Example:
<validators>
     <field name=" homepage">
        <field-validator type="fieldexpression">
            <param name="expression"> homepage.indexOf('opensymphony.com') == -1</param>
            <message>Please provide an OpenSymphony website</message>
        </field-validator>
    </field>
</validators>
-------------------------------------------------------------------------------
使用第二种验证方法可以将验证的内容写在所在的表单元素的field标签中,哈哈,这个很好,以后也方便改写。(第一种方法也行,但要分开写!看着不爽阿!)

这东东用的是OGNL语法写表达式的,可以看 http://www.ognl.org/ 参考它的写法。

以下的网址讨论了一些写表达式时常出现的问题:
http://wiki.opensymphony.com/display/XW/ExpressionValidator+Tips

你可能感兴趣的:(validation)