如何在 Struts2 中国际化集合的转换错误消息?
特定于单个字段的相关问题已解决 此处。但是如何自定义集合字段的转换错误消息呢?
这是一个例子: 在jsp页面上,我有一个集合类型的字段:
...
<s:iterator value="items" status="m">
<s:hidden name="selitmems[%{#m.index}].id" value="%{id}"/>
<s:textfield name="selitmems[%{#m.index}].quant" size="10"/>
</s:iterator>
项目的类型是:List
; Selitems 的类型是 List
。 我希望 selitmems[].quant 属性为整数类型。如果最终用户为第一项填写了类似“abc”的字符串,则默认错误消息为:
字段“selitmems[0].quant”的字段值无效。
上面的消息不是我想要的。就我而言,无论具体选择的项目如何,我都更愿意将错误消息概括如下:
请输入整数。
当然,如果错误消息可以根据具体项目而有所不同,那就太好了:
请输入第一项的整数。
我尝试在属性文件中添加一些键,例如“selitmems[0].quant”或“selitmems”,但无法获得结果。当我仍然可以重用内置的类型转换函数时,有没有办法自定义struts2中集合字段的错误消息?
A related problem that is specific to single field has been solved here. But how to customize a collection field's conversion error message?
Here is an example:
On a jsp page, I have a field in Collection type:
...
<s:iterator value="items" status="m">
<s:hidden name="selitmems[%{#m.index}].id" value="%{id}"/>
<s:textfield name="selitmems[%{#m.index}].quant" size="10"/>
</s:iterator>
The items' type is: List<Item>
; the selitems' type is List<SelItem>
.
I want selitmems[].quant property to be an integer type. If a string like "abc" is filled in for the first item by an end user, the default error message is:
Invalid field value for field "selitmems[0].quant".
The above message is not what i want. In my case, I would prefer to generalize the error message as follows regardless of the specific selected item:
Please input integers for the items.
Of course it would be great if the error message can vary according to the specific item:
Please input an integer for the first item.
I have tried to add some keys like "selitmems[0].quant" or "selitmems" in the properties file, but can't get the result. Is there way to customize the error message for a collection field in struts2 when I can still reuse the built-in type conversion functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用“标签”属性。您可以用它玩一些...有趣的游戏,例如(未经测试,但接近):
属性文件将包含:
您可以按照描述更改转换错误消息 此处,尽管这可能不是您想要做的。
(众所周知,当错误的转换也未通过字段的“真实”验证时,我会完全删除转换拦截器,并让默认转换器或自定义转换器处理转换错误。)
嗯嗯...你可以玩一些疯狂的游戏OGNL 和替换。
Try using the "label" attribute. You can play some... interesting games with this, such as (untested, but close):
The property file would contain:
You can change the conversion error message as described here, although this may not be precisely what you want to do.
(I've been known to remove the conversion interceptor altogether and let either the default converters or custom converters handle conversion errors when a bad conversion also fails the field's "real" validation.)
Mm hmm... you can play some crazy games with OGNL and substituion.
将类似的内容放入您的属性文件中。
然后,
selitmems[0].quant
、selitmems[1].quant
、selitmems[2].quant
转换错误都会返回相同的消息。Put something like this in your properties file.
Then
selitmems[0].quant
,selitmems[1].quant
,selitmems[2].quant
conversion errors all return the same message.