jsf h:messages/ h:message for specific clientId
I would like to display different FacesMessages with programmatically set clientIds. In my view I used
<h:outputText value="warnMessages #{facesContext.getMessageList('warnMessages')}" />
<h:outputText value="validationMessages #{facesContext.getMessageLis开发者_StackOverflowt('validationMessages')}" />
for debugging. The messages are all there. But...
<h:messages for="warnMessages" />
<h:message for="warnMessages" />
doesn't display anything. Only
<h:messages />
works. But I would like to have different message-boxes displayed together. How can I do that? Thanks
Marcel
It look like that you're abusing client IDs to separate custom/global messages from normal validation messages.
You shouldn't do that. If you want to display a custom/global message, just don't specify any client ID. I.e., set it to null
.
context.addMessage(null, facesMessage);
This way they will all appear in
<h:messages globalOnly="true" />
And use the renmant of the messages the usual way.
<h:inputText id="foo" required="true" />
<h:message for="foo" />
or
<h:messages globalOnly="false" />
The h:messages
display all messages, also those with missing client id. So the for
attribute is not allowed.
The for
attribute in h:message
must match the id
attribute of another component: in your example you have to set id="warnMessages"
in whatever h:inputText
you want.
Adding custom messages to JSF pages is not that difficult. But you will have to do some effort to do all the required stuff.
e.g.
<h:outputText value="#{bean.warnMessages}" />
<h:outputText value="#{bean.infoMessages}" />
and in bean class
...
public class Bean {
...
public String warnMessages() {
// concatenate (and add new lines to) all the WARNING string messages, with
// html+css formatting, together and
return warnings;
}
public String infoMessages() {
// concatenate (and add new lines to) all the INFO string messages, with
// html+css formatting, together and
return infos;
}
}
Also remember to update the output text messages using ajax upon some event, which will call the bean's warnMessages()
and infoMessages()
methods.
精彩评论