Tuesday, January 3, 2012

ADF: How to show multi-lines af:message

In some cases the user may need to show a multi-lines message, in this post I will explain how to do this.
af:message allows us to include a formatted text in the message, this can be done by surrounding the message text with <html> tags.

So to show a multi-lines message, we can use <p> tag  in our message text. The method below displays three lines message.
public String shwoMultiLinesMessage() {
        StringBuilder builder = new StringBuilder(“<html> <body>”);
        builder.append(“<p>First Line</p>”);
        builder.append(“<p>Second Line</p>”);
        builder.append(“<p>Third Line</p> “);
        builder.append(“</body> </html>”);
        FacesMessage fm = new FacesMessage(builder.toString());
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, fm);
        return null;
    }
The result is af:message with three lines as shown in the image below:
multi-lines message
Reference: Credits goes to original author Mohamed Jabr.