为不同的复合组件指定不同的错误消息 [英] Specifying different error messages for different composite components

查看:119
本文介绍了为不同的复合组件指定不同的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以为不同种类的复合组件提供必需的消息?

Is there a way to have REQUIRED messages for different kinds of composite components?

我希望能够在消息束中说出类似以下内容

I'd like to be able to say something like the following in a message bundle

javax.faces.component.UIInput.REQUIRED=Please enter a value for {0}
com.anon.sandbox.jsf.component.NameField.REQUIRED=ENTER A {} OR DIE
com.anon.sandbox.jsf.component.EmailField.REQUIRED=You forgot to enter a {0} address

我可以通过为每种输入字段实现自定义验证器来做到这一点,但是鉴于我要自定义的唯一行为是错误消息,这似乎有些过分.

I could do this by implementing custom validators for each kind of input field, but given that the only behaviour I want to customise is the error message this seems like overkill.

推荐答案

使用requiredMessage属性指定它们.

<h:inputText ... requiredMessage="#{cc.resourceBundleMap.requiredMessage}" />

对于您不知道的情况,#{cc.resourceBundleMap}引用特定于复合组件的资源束,该资源束由与同一个文件夹中的复合组件具有相同文件名的.properties文件标识.

For the case you didn't knew that, the #{cc.resourceBundleMap} refers the composite component specific resource bundle which is identified by a .properties file with the same filename as the composite component in the same folder.

因此,假设您有一个复合组件文件foo.xhtml,然后可以在同一文件夹中的foo.properties(因此,是foo_en.propertiesfoo_es.properties等)中指定本地化的消息.然后,这些本地化的消息可由前面提到的#{cc.resourceBundleMap}提供.

So, imagine that you've a composite component file foo.xhtml, then you can specify the localized messages in foo.properties (and consequently foo_en.properties, foo_es.properties, etc) which reside in the very same folder. Those localized messages are then available by the aforementioned #{cc.resourceBundleMap}.

这篇关于为不同的复合组件指定不同的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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