FacesMessage Enhancements

PrimeFaces 3.3 brings handy features regarding displaying facesmessages. These features were already available in PrimeFaces Extensions Project with a different implementation and there was a popular community request to promote these ideas to PrimeFaces. Also during a team meeting last week, our consultants mentioned that they needed at least two of these during their work onsite for a client where they provide onsite assistance on PrimeFaces. Long story short, here are the 3 additions;

Targetable Messages

There maybe times where you need to target a FacesMessage to a specific component, for example suppose you have p:growl and p:messages on same page and you need to display some messages on growl, some on p:messages. We have added grouping defined via for attribute to implement these;

<p:messages for="somekey" /> <p:growl for="anotherkey" /> <p:growl globalOnly="true" /> 1 2 3 <p:messages for = "somekey" /> <p:growl for = "anotherkey" /> <p:growl globalOnly = "true" />

context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces Rocks")); context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "Always bet on Prime")); context.addMessage("anotherkey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces is developed by Chuck Norris")); context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample warn message", "Optimus Prime will be back!")); 1 2 3 4 5 6 context . addMessage ( "somekey" , new FacesMessage ( FacesMessage . SEVERITY_INFO , "Sample info message" , "PrimeFaces Rocks" ) ) ; context . addMessage ( "somekey" , new FacesMessage ( FacesMessage . SEVERITY_INFO , "Sample info message" , "Always bet on Prime" ) ) ; context . addMessage ( "anotherkey" , new FacesMessage ( FacesMessage . SEVERITY_INFO , "Sample info message" , "PrimeFaces is developed by Chuck Norris" ) ) ; context . addMessage ( null , new FacesMessage ( FacesMessage . SEVERITY_INFO , "Sample warn message" , "Optimus Prime will be back!" ) ) ;

So first two messages will be displayed by messages, second one by first growl and last with second growl.

Severity Levels

Using severity attribute of messages components, you can defined which severities can be displayed by the component. So for instance, we are now able to display errors on messages and infos on a growl that fades out.

<p:messages severity="error" /> <p:growl severity="info, warn" /> <p:growl /> //default 1 2 3 <p:messages severity = "error" /> <p:growl severity = "info, warn" /> <p:growl /> / / default

Escaping

Until now message components, escape the html content in FacesMessage summary and detail, just set escape to false to display html inside messages.

context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample warn message", "PrimeFaces is developed by <strong>Chuck Norris!</strong>")); 1 context . addMessage ( null , new FacesMessage ( FacesMessage . SEVERITY_INFO , "Sample warn message" , "PrimeFaces is developed by <strong>Chuck Norris!</strong>" ) ) ;

<p:messages escape="false" /> 1 <p:messages escape = "false" />

That’s it! Hope you find these features handy, we’ve already started using targeting and severity levels in a project!