删除 struts 2 中的转换验证消息或使其通用 [英] remove conversion validation message in struts 2 or make it general

查看:20
本文介绍了删除 struts 2 中的转换验证消息或使其通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 struts2 应用程序中,我有一个名为carrierNo 的字段,它接受整数,当我将字符串放入其中时,会给出以下验证错误消息:

In my struts2 application I have field named carrierNo that accepts integer, when i put string in it gives me this validation error message:

    *Invalid field value for field "carrierNo".*

我可以像这样在属性文件中自定义此错误消息

i can customize this error message in the properties file like this

    invalid.fieldvalue.carrierNo=this field does not accept characters

但我不想为我的 Web 应用程序中的每个非字符串字段编写自定义消息,我想让它通用,我尝试了以下操作,但没有用

but i don't want to write a customized message for every non String field in my web application, i want to make it general, i tried the following but it did not work

  invalid.fieldvalue.%{getText(fieldName)}=this field does not accept characters

如果没有办法进行通用,请帮助我完全禁用此消息.然后我将使用转换字段验证器和我在属性文件中定义的单个消息.

if there is no way to make general, please help me disable this message at all. then i will use converstion field validator with single message that i define in the properties file.

所以我的请求是帮助我使这个 invalid.fieldvalue.carrierNo 像这种形式的一般内容 invalid.fieldvalue.%{getText(fieldName)}

so my request is to help me make this invalid.fieldvalue.carrierNo general something like this form invalid.fieldvalue.%{getText(fieldName)}

或禁用此错误消息的显示字段carrierNo"的字段值无效.

or disable the display of this error message Invalid field value for field "carrierNo".

推荐答案

您可以创建自己的 ConversionErrorInterceptor 实现,它会找出失败字段的类别并获取您的自定义消息.

You could create your own implementation of ConversionErrorInterceptor which finds out the class of failed field and gets your custom message.


请参阅 ConversionErrorInterceptor 的源代码.例如,您可以在 intercept 方法


See source code for ConversionErrorInterceptor. For example you could do something like this in your custom interceptor inside intercept method

// get field by name from action
Field f = invocation.getAction().getClass().getDeclaredField(propertyName);
// get type of field
Class clz = f.getType();

String message = LocalizedTextUtil.findDefaultText(XWorkMessages.DEFAULT_INVALID_FIELDVALUE + "." + clz,
                    invocationContext.getLocale());

在你的messages.properties文件中放入xwork.default.invalid.fieldvalue.intxwork.default.invalid.fieldvalue.float

And in your messages.properties file put xwork.default.invalid.fieldvalue.int, xwork.default.invalid.fieldvalue.float, etc.

这篇关于删除 struts 2 中的转换验证消息或使其通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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