By default, every JSF FacesMessage is presented in a single row. I would like to add a HTML line break <br /> to the message itself, so that the message is shown neatly. I tried it like below
message = new FacesMessage("test<br/>test");
However, it got escaped by JSF and is shown as literal text. How can I add HTML code to a FacesMessage without it getting escaped?
In theory, you want an
escapeattribute for theh:messagescomponent like theh:outputTexthas. You’re not the only one who wants this, this is requested before more than often, but it’s a WONTFIX according the JSF guys.You have several options:
Use
\ninstead of<br>and apply CSS accordingly (easiest).Create a custom renderer which extends
MessageRenderer(bit harder, but nice if you want to cover more HTML than only linebreaks).Gather the messages yourself in some
Listin a bean and display them using<t:dataList>, or when you’re already using Facelets instead of JSP, using<ui:repeat>. This way you can use<h:outputText escape="false">to display the individual messages.Or, when you’re already on JSF 2.0, just iterate over
FacesContext#getMessageList()yourself. Each item gives you aFacesMessageback which in turn offers several getters. You could then display the summary in a<h:outputText escape"false" />.Or, when you’re using JSF utility library OmniFaces, use its
<o:messages>component instead which has support forescapeattribute.