How to use HTML character entities inside EL in JSF?

你说的曾经没有我的故事 提交于 2019-12-10 14:54:32

问题


I want to use the em dash in a value attribute for an h:link component.

Here is my attempt (currenctly not working):

<h:link value="#{somethingHere} &mdash; #{anotherHere}">
    <f:param name="identifier" value="#{somethingHere.identifier}" />
</h:link>

This results in a FaceletsException:

FaceletException: Error Parsing /index.xhtml: Error Traced[line: 13]
                The entity "mdash" was referenced, but not declared.
at com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:394)
...

I know I can use an HTML anchor instead, but is there a way to do it inside an EL expression? What is the correct way to do this?


回答1:


Facelets is XML based and processed by a XML parser. The &mdash; is a HTML entity and not recognized in XML. Only the five listed in this Wikipedia page, &quot;, &amp;, &apos;, &lt; and &gt;, are recognized in XML.

Facelets/XML uses by default already UTF-8, so you could just put the actual character plain/unencoded in the template (provided that the editor is able to save the file as UTF-8).

<h:link value="#{somethingHere} — #{anotherHere}">

If this isn't an option for some reason, then you could instead use a numeric character reference in the format &#nnnn;, like as one would use &#160; to represent a &nbsp; in XML. You can find the numeric character reference in fileformat.info: Unicode Character 'EM DASH' (U+2014)

Encodings

HTML Entity (decimal) &#8212;

So, this should do for you:

<h:link value="#{somethingHere} &#8212; #{anotherHere}">

An alternative, which should satisfy the exact error message more, is to declare the entity reference explicitly yourself in the doctype.

<!DOCTYPE html [
    <!ENTITY mdash "&#8212;"> 
]>

But this isn't the general recommendation/approach as you'd need to repeat this over every single XML file wherein the character is been used.



来源:https://stackoverflow.com/questions/11992707/how-to-use-html-character-entities-inside-el-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!