19

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gekrish
  • 2,201
  • 11
  • 29
  • 46
  • Isn't this the same question as in http://stackoverflow.com/questions/3186659/showing-the-jsf-error-messages ? – pakore Jul 07 '10 at 13:49

4 Answers4

32

In theory, you want an escape attribute for the h:messages component like the h:outputText has. 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:

  1. Use \n instead of <br> and apply CSS accordingly (easiest).

    #messages td { white-space: pre; }
    
  2. Create a custom renderer which extends MessageRenderer (bit harder, but nice if you want to cover more HTML than only linebreaks).

  3. Gather the messages yourself in some List in 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.

  4. Or, when you're already on JSF 2.0, just iterate over FacesContext#getMessageList() yourself. Each item gives you a FacesMessage back which in turn offers several getters. You could then display the summary in a <h:outputText escape"false" />.

    <ul>
        <ui:repeat value="#{facesContext.messageList}" var="facesMessage">
            <li>
                <h:outputText value="#{facesMessage.summary}" escape="false" />
            </li>
        </ui:repeat>
    </ul>
    
  5. Or, when you're using JSF utility library OmniFaces, use its <o:messages> component instead which has support for escape attribute.

    <o:messages escape="false" />
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    When you are on JSF 2.0, you can also use the OmniFaces [`` component](http://showcase.omnifaces.org/components/messages) with the `escape="false"` attribute. – Adam Jan 16 '15 at 10:28
  • @BalusC Do you want to link the WONTFIX issue or not? I'm not sure. – alexander Feb 23 '16 at 13:34
4

Primefaces 5.3 support HTML on FacesMessages, just setting escape="false" at messages component:

<p:messages escape="false"/>

The p:growl supports this too btw.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
2

I followed this blog.

StringBuilder sb = new StringBuilder("<html><body>");
sb.append("<p>A list of messages are:</p>");
for(String str : listMessages){
      sb.append("Message: ").append(str).append("<br/>");
}  
sb.append("</body></html>");

FacesMessage message = new FacesMessage(sb.toString());
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage("", message);

The key is not to miss the <html> and <body> tags and closing them properly like a valid HTML. Otherwise HTML tags appear as text on the dialog.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
uutsav
  • 389
  • 5
  • 16
0

Use a CSS class to format the message, as BalusC told you in your previous question:

Showing the JSF Error Messages

Community
  • 1
  • 1
pakore
  • 11,395
  • 12
  • 43
  • 62