如何在Struts 2中正确验证字段 [英] How to validate fields properly in Struts 2

查看:190
本文介绍了如何在Struts 2中正确验证字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我在这里做错了什么:

I would like to know what I'm doing wrong here :

<validators>
    <field name="cli.idCli" >
        <field-validator type="requiredString" short-circuit="true">
            <message>Required field (*)</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">8</param>
             <param name="maxLength">8</param>
             <message>ID must has 8 characters</message>
        </field-validator>
    </field>
</validators>

因为,实际上我收到了这个错误:

because, actually i'm getting this error:


错误
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager
加载文件时出现异常
... / action / ServiceAction-findPets-validation。 xml无效验证
类型:requiredString - field-validator

ERROR com.opensymphony.xwork2.validator.AnnotationActionValidatorManager Caught exception while loading file .../action/ServiceAction-findPets-validation.xml Invalid validation type: requiredString - field-validator

另外,我想知道是否进行ajax调用(和发送JSON数据)我可以验证这些数据的存在吗?我的意思是通过.xml验证器。此外,如果我实现 Validateable 接口,每次请求到达此操作时,此方法将被触发?有没有办法指定它是哪种操作方法?

Also, I would like to know if doing ajax calls (and sending JSON data) I can validate the existence of these data ? I mean through .xml validators. Moreover, if I implement Validateable interface, each time that a request arrives to this action this method is going to be triggered? Is there a way to specify for which action methods is it?

推荐答案

名称区分大小写,你应该使用

The name is case sensitive, you should use

<field-validator type="requiredstring" short-circuit="true">

数据可以在填充到操作后进行验证。您可以使用 * - validation.xml 以编程方式通过声明式方式执行此操作验证()

The data can be validated after it's populated to the action. Either you do it in a declarative way using *-validation.xml or programmatically via validate().

您可以配置验证拦截器以在这两种方式之间进行选择,因为默认情况下验证()在声明性验证完成后运行,即使它们是短路的。

You can configure validation interceptor to choose between these two ways, because by default validate() runs after declarative validations are done even if they are short-circuit.

考虑使用 alwaysInvokeValidate 参数。如果将别名验证文件用作 ActionClass-alias-validation.xml ,则仅对此操作别名运行验证。映射操作方法时,可以使用此别名。

Consider using alwaysInvokeValidate parameter. If you use aliased validation files as ActionClass-alias-validation.xml then validations are run for this action alias only. You can use this alias when you map your action methods.

这篇关于如何在Struts 2中正确验证字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆