为不同的复合组件指定不同的错误消息

发布于 2025-01-04 17:50:50 字数 377 浏览 3 评论 0原文

有没有办法为不同类型的复合组件提供 REQUIRED 消息?

我希望能够在消息包中说出如下内容

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

,我可以通过为每种输入字段实现自定义验证器来做到这一点,但考虑到我想要自定义的唯一行为是错误消息,这似乎有点矫枉过正。

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.

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

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

发布评论

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

评论(1

很糊涂小朋友 2025-01-11 17:50:50

使用 requiredMessage 属性来指定它们。

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

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

因此,假设您有一个复合组件文件 foo.xhtml,那么您可以在 foo.properties 中指定本地化消息(因此 foo_en.properties< /code>、foo_es.properties 等)位于同一文件夹中。然后,这些本地化消息可通过上述 #{cc.resourceBundleMap} 获得。

Use the requiredMessage attribute to specify them.

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

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.

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}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文