简体   繁体   中英

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:

在此处输入图片说明

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

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

Presentation:

在此处输入图片说明

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM