问题描述
我的 struts 2 形式之一中有一个字段长度".length"的数据类型是double".我在 XML 文件中应用了双重"验证.但是当我在长度"文本字段中键入字母时,它显示的错误消息为
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"
我不希望此消息显示为这样.此消息是由 struts 2 本身生成的,不是我输入的.我猜,这条消息是在数据转换失败时出现的.我还应用了转换"验证器,但仍然显示上述错误消息.请提出解决方案.
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.
提前致谢.
推荐答案
你很幸运.此文本可自定义.
You're in luck. This text is customizable.
文本在 xwork jar 中的 xwork-messages.properties 中定义.您可以通过将以下内容添加到您的全局 i18n 资源包来覆盖它:
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}".
如您所料,所有类型转换失败都会出现此错误消息.XWorkConverter
类对此有一些有用的 javadoc:
As you guessed, this error message occurs for all type conversion failures. The XWorkConverter
class has some useful javadoc about this:
在类型转换期间发生的任何错误可能希望也可能不希望报告.例如,报告输入abc"不存在.无法转换为数字可能很重要.另一方面,报告无法将空字符串"转换为数字可能并不重要 - 特别是在难以区分用户未输入值与输入空白的 Web 环境中价值.
默认情况下,所有转换错误都使用通用 i18n 键 xwork.default.invalid.fieldvalue 报告,您可以覆盖该键(默认文本是 Invalid field value for field "xxx",其中 xxx 是您的全局 i18n 资源包中的字段名称.
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.
但是,有时您可能希望在每个字段的基础上覆盖此消息.您可以使用模式 invalid.fieldvalue.xxx 添加与您的操作 (Action.properties) 关联的 i18n 键,其中 xxx 是字段名称.
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.
重要的是要知道这些错误中没有一个实际上是直接报告的.相反,它们被添加到 ActionContext 中名为 conversionErrors 的映射中.然后可以通过多种方式访问此地图并相应地报告错误.
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.
这篇关于验证 struts 2 中的双字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!