错误解析/page.xhtml: 错误跟踪[第42行]引用了实体“ nbsp”,但没有声明它

我想在我的 JSF 页面中使用非中断空格。我知道,在纯 HTML 中我可以使用  来实现这一点,而且这个工作得很好。然而,当我将这些  放入 Faclets 页面时,它会出现以下错误:

错误解析/page.xhtml: 错误跟踪[第42行]引用了实体“ nbsp”,但没有声明它。

这是怎么引起的,我该怎么解决?

76393 次浏览

Facelets is a XML based view technology. XML has only five predefined entities. The   is not among them. It works only when used in plain HTML or in legacy JSP (note: it doesn't work in JSPX as that's also XML based!).

To fix this, you either need to declare the entity yourself in the doctype of the very XHTML file containing the   entity,

<!DOCTYPE html [
<!ENTITY nbsp "&#160;">
]>

or, better, use the (hexa)decimal notation instead:

&#xA0; or &#160;

In an average IDE it should be trivial to perform a "find and replace in all files" and replace every occurrence of &nbsp; by &#xA0;.

Try using &amp;#160;. for more information on entities you can refer following url HTML ISO-8859-1 Reference

  1. <![CDATA[&nbsp;]]> should work fine. This does not work inside attribute value, though.

  2. Alternatively, as long as &amp; is a predefined entity in XML, you could try &amp;nbsp; - worked for me with XML based JSP. This should work within attributes as well.

add

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

to the top of the file

For me the following doctype enables &nbsp;:

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