如何在JSF的EL中使用HTML字符实体? [英] How can I use HTML character entities inside EL in JSF?
问题描述
我想在h:link
组件的value属性中使用 em破折号
I want to use the em dash in a value attribute for an h:link
component.
这是我的尝试(当前无法正常工作):
Here is my attempt (currently not working):
<h:link value="#{somethingHere} — #{anotherHere}">
<f:param name="identifier" value="#{somethingHere.identifier}" />
</h:link>
这将导致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)
...
我知道我可以改用HTML锚,但是有一种方法可以在表达语言(EL)表达式?正确的方法是什么?
I know I can use an HTML anchor instead, but is there a way to do it inside an Expression Language (EL) expression? What is the correct way to do this?
推荐答案
Facelets是基于XML的,并由XML解析器进行处理. —
是HTML实体,不能用XML识别. 此Wikipedia页面,"
,&
,<
和>
,以XML识别.
Facelets is XML based and processed by a XML parser. The —
is a HTML entity and not recognized in XML. Only the five listed in this Wikipedia page, "
, &
, '
, <
and >
, are recognized in XML.
Facelets/XML默认情况下已使用UTF-8,而HTML实体基本上是UTF-8之前时代的遗留物,而在UTF-8文档中则没有必要,因此您可以将实际字符纯/未编码放置在模板(前提是编辑器能够将文件另存为UTF-8).
Facelets/XML uses by default already UTF-8, and HTML entities are basically a leftover of pre-UTF-8 era and not necessary in UTF-8 documents, 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}">
到
<h:link value="#{somethingHere} — #{anotherHere}">
如果由于某种原因这不是一个选择,则可以改用&#nnnn;
格式的数字字符引用,就像使用 
表示XML中的
一样.您可以在fileformat.info中找到数字字符引用: Unicode字符'EM DASH '(U + 2014)
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  
to represent a
in XML. You can find the numeric character reference in fileformat.info: Unicode Character 'EM DASH' (U+2014)
编码
HTML实体(十进制)
—
所以,这应该为您做
<h:link value="#{somethingHere} — #{anotherHere}">
一种替代方法,应该更多地满足确切的错误消息,那就是在doctype中自己明确声明实体引用.
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 "—">
]>
但这不是一般的建议/方法,因为您需要在每个使用该字符的XML文件中重复此操作.
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.
这篇关于如何在JSF的EL中使用HTML字符实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!