javax.faces.view.facelets.FaceletException: Error Parsing /template.xhtml: The entity "nbsp" was referenced, but not declared

StackOverflow https://stackoverflow.com/questions/21754475

Question

In my Facelets template I used a XHTML doctype:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

I'd like to change this to HTML5 doctype as per the general recommendation:

<!DOCTYPE html>

However, JSF threw a parsing error:

javax.faces.view.facelets.FaceletException: Error Parsing /template.xhtml: Error Traced[line: 42] The entity "nbsp" was referenced, but not declared.

The HTML &nbsp; entity is not parsed then. The entity is used as follows:

<p:menuitem value="&nbsp;My menuitem">

How is this caused and how can I solve it?

Was it helpful?

Solution

The HTML5 doctype has no predefined entities. It assumes that the entire document is already UTF-8 aware. In other words, you can "safely" put the real non breaking space character in the HTML5 document. However, as it's not trivial to enter it in a visible way using a regular US keyboard, it's understandable that many developers prefer using an entity for it.

Facelets in turn supports as being a XML based view technology only five predefined XML entities &lt;, &gt;, &amp;, &quot; and &apos;. The &nbsp; is not among them.

You've 2 options (apart from the 3rd option to just print the character straight in the document):

  1. Declare the &nbsp; entity yourself in the doctype:

    <!DOCTYPE html [
        <!ENTITY nbsp "&#xA0;"> 
    ]>
    
  2. Or, use the (hexa)decimal notation instead:

    &#xA0; or &#160;
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top