Specifying different error messages for different composite components

霸气de小男生 提交于 2019-12-01 07:24:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9247124/specifying-different-error-messages-for-different-composite-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!