验证 struts 2 中的双字段

发布于 2024-10-31 18:44:13 字数 294 浏览 2 评论 0原文

我的 struts 2 表单之一中有一个“长度”字段。 “length”的数据类型是“double”。我已经在 XML 文件中应用了“双重”验证。但是,当我在“长度”文本字段中键入字母时,它会显示错误消息,因为

Invalid field value for field "length"

我不希望像这样显示此消​​息。这个消息是struts 2本身生成的,不是我输入的。我猜想,此消息是在数据转换失败时出现的。我还应用了“转换”验证器,但仍然显示上述错误消息。请提出解决方案。

提前致谢。

I have a field "length" in one of my struts 2 form. The data-type of "length" is "double". I have applied the "double" validation in XML file. But when I key-in alphabets in the "length" text field, it shows the error message as

Invalid field value for field "length"

I don't want this message to be shown like this. This message is generated by struts 2 itself and not entered by me. I guess, this message comes as the conversion of data fails. I also applied the "conversion" validator, but the above error message is still showing up. Please suggest the solution.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦晓ヶ微光ヅ倾城 2024-11-07 18:44:13

你很幸运。该文本是可定制的。

该文本在 xwork jar 中的 xwork-messages.properties 中定义。您可以通过将以下内容添加到全局 i18n 资源包来覆盖它:

xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".

正如您所猜测的,所有类型转换失败都会出现此错误消息。 XWorkConverter 类有一些关于此的有用 javadoc:

类型转换期间发生的任何错误可能希望也可能不希望被报告。例如,报告输入“abc”无法转换为数字可能很重要。另一方面,报告空字符串“”无法转换为数字可能并不重要 - 特别是在很难区分用户未输入值和输入空白值的 Web 环境中。

默认情况下,所有转换错误均使用通用 i18n 键 xwork.default.invalid.fieldvalue 报告,您可以覆盖该键(默认文本为字段“xxx 的无效字段值”) “,其中 xxx 是字段名称)在您的全局 i18n 资源包中。

但是,有时您可能希望在每个字段的基础上覆盖此消息。您可以通过使用模式 invalid.fieldvalue.xxx 添加仅与您的操作 (Action.properties) 关联的 i18n 键来实现此目的,其中 xxx 是字段名称。

重要的是要知道这些错误实际上都不是直接报告的。相反,它们被添加到 ActionContext 中名为 conversionErrors 的映射中。有多种方法可以访问此地图并相应地报告错误。

You're in luck. This text is customizable.

The text is defined in xwork-messages.properties in the xwork jar. You can override it by adding the following to your global i18n resource bundle:

xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".

As you guessed, this error message occurs for all type conversion failures. The XWorkConverter class has some useful javadoc about this:

Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value.

By default, all conversion errors are reported using the generic i18n key xwork.default.invalid.fieldvalue, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle.

However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name.

It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.

风柔一江水 2024-11-07 18:44:13

试试这个,

<field name="percentage">
    <field-validator type="double">
        <param name="minExclusiveExpression">${minExclusiveValue}</param>
        <param name="maxExclusiveExpression">${maxExclusiveValue}</param>
        <message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
    </field-validator>
</field>

try this,

<field name="percentage">
    <field-validator type="double">
        <param name="minExclusiveExpression">${minExclusiveValue}</param>
        <param name="maxExclusiveExpression">${maxExclusiveValue}</param>
        <message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
    </field-validator>
</field>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文