如何在Struts 2中正确验证字段

编程入门 行业动态 更新时间:2024-10-24 20:23:21
本文介绍了如何在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中正确验证字段

本文发布于:2023-10-16 11:53:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   正确   如何在   Struts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!