如何在JSF的EL中使用HTML字符实体? [英] How can I use HTML character entities inside EL in JSF?

查看:110
本文介绍了如何在JSF的EL中使用HTML字符实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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} &mdash; #{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解析器进行处理. &mdash;是HTML实体,不能用XML识别. 此Wikipedia页面&quot;&amp;&lt;&gt;,以XML识别.

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默认情况下已使用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} &mdash; #{anotherHere}">

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

如果由于某种原因这不是一个选择,则可以改用&#nnnn;格式的数字字符引用,就像使用&#160;表示XML中的&nbsp;一样.您可以在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 &#160; to represent a &nbsp; in XML. You can find the numeric character reference in fileformat.info: Unicode Character 'EM DASH' (U+2014)

编码

HTML实体(十进制)&#8212;

所以,这应该为您做

<h:link value="#{somethingHere} &#8212; #{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 "&#8212;"> 
]>

但这不是一般的建议/方法,因为您需要在每个使用该字符的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆