Search code examples
jsfmessageomnifacesapostrophe

How to display apostrophe ' in faces message added via OmniFaces Messages#add


I am using p:messages for display error no UI in primefaces XHTML page. I want to display String like Employee's. When I am trying to use OmniFaces Messages utility, it is not showing. For more detail look code below.

XHTML:

<p:messages id="globalMessages" autoUpdate="false" closable="true"
                escape="true" showDetail="true"/>

Bean:

Messages.add(FacesMessage.SEVERITY_ERROR, "global", "employee's");

Presentation:

enter image description here

It works when I use plain FacesContext#addMessage():

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "PrimeFaces Rocks employ's"));

Presentation:

enter image description here

But I have to use Messages.add(FacesMessage.SEVERITY_ERROR, "global", "employee's");

How is this caused and how can I display the single quote in the message?


Solution

  • As stated in javadoc, the default resolver of OmniFaces Messages uses MessageFormat API to format messages, exactly like as how <h:outputFormat> and resource bundles work.

    In MessageFormat API, the single quote is a special character and needs to be escaped with another one if you want to represent it as-is.

    Messages.add(FacesMessage.SEVERITY_ERROR, "global", "employee''s");
    

    Alternative is to use a curly quote instead.

    Messages.add(FacesMessage.SEVERITY_ERROR, "global", "employee’s");
    

    Or to register a custom message resolver which does nothing with the message.

    Messages.setResolver(new Messages.Resolver() {
         public String getMessage(String message, Object... params) {
             return message;
         }
     });
    

    But then you can't use message parameters anymore.


    Update: I have just improved the Messages utility to not perform any formatting if there are no message parameters in first place. You can see the enhancement in this commit. It's available in today's latest 2.5-SNAPSHOT.