PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

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

 July 12, 2022     apostrophe, jsf, message, omnifaces     No comments   

Issue

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.



Answered By - BalusC
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing