为什么在JSF中对与号(&)进行编码?有没有解决的办法? [英] Why do ampersands (&) need to be encoded in JSF? Is there a way around this?

查看:57
本文介绍了为什么在JSF中对与号(&)进行编码?有没有解决的办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在具有&&在其中,我最终不得不将其编码为&&

I often have Javascript on my JSF XHTML pages that has && in it, which I end up having to encode as &&

例如,当我在JSF XHTML页面文件中放置以下内容时:

For example, when I place the following in my JSF XHTML page file:

I am an & sign

我得到了错误:

The entity name must immediately follow the '&' in the entity reference

解决此问题的一种方法似乎是更改&"到&,我觉得只写'&'是不合适的.

One wayto fix this appears to be to change the '&' to & which I find undesirable to just writing '&'.

对于使用&"的情况,在Javascript中,我可以将Javascript包装在CDATA标记中;当包裹在CDATA标签中时,我可以写&"不必将其转为&,这是一个很好的解决方法,可以在我的页面上使用更具可读性的Javascript代码.

It also appears that for cases where I use the '&' in Javascript, I can wrap the Javascript in CDATA tags; when wrapped in CDATA tags, I can then write '&' without having to escape it as &, which is a good workaround to be able to have more readable Javascript code on my page.

但是当我想使用文字'&'时会发生什么?当页面不在<script>标记内时,是否可以轻松地将代码包装在CDATA标记中?我是否必须总是 逃脱&"在这些情况下为&amp;?

But what happens when I want to use the literal '&' elsewhere on the page when it is not within <script> tags and therefore cannot as easily wrap the code in CDATA tags? Must I always escape '&' as &amp; for these cases?

请注意,尝试使用的功能来逃避值,并且似乎无法解决问题

Note trying to use 's ability to escape values and do not seem to be able to fix the issue

推荐答案

Facelets是一种基于XML的视图技术.如果要按字面意义显示XML解析器中经过特殊处理的任何字符,都需要对XML进行转义.其中包括<&. <表示类似<foo>的XML标签的开始,而&表示像&#38;这样的XML实体的开始. <必须转义为&lt;,而&转义为&amp;.

Facelets is a XML based view technology. Any characters which have special treatment by the XML parser needs to be XML-escaped when the intent is to present them literally. That covers among others < and &. The < indicates the start of a XML tag like so <foo> and the & indicates the start of a XML entity like so &#38;. The < must be escaped as &lt; and the & as &amp;.

未在Facelets中转义它们会导致以下<

Not escaping them in Facelets would result in the following exception for <

javax.faces.view.facelets.FaceletException:错误解析/test.xhtml:错误跟踪[行:42]元素的内容必须包含格式正确的字符数据或标记.

javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 42] The content of elements must consist of well-formed character data or markup.

,以下是&

javax.faces.view.facelets.FaceletException:错误分析/test.xhtml:错误跟踪[行:42]实体名称必须紧跟在&"之后在实体参考中.

javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 42] The entity name must immediately follow the '&' in the entity reference.

这与JavaScript没有特别关系,这适用于整个视图,包括纯文本".这些字符恰好也是JavaScript运算符.没有办法解决这个问题,这就是指定XML的方式.但是,在JavaScript中,还有另一种避免转义或使用CDATA块的方法:只需将JS代码放在自己的.js文件中,即可通过<script><h:outputScript>加载.

This is not specifically related to JavaScript, this applies to the entire view, including "plain text". Those characters just happen to be JavaScript operators as well. There's no way to go around this, that's just how XML is specified. In JavaScript, there's however one more way to avoid escaping or using CDATA blocks: just put that JS code in its own .js file which you load by <script> or <h:outputScript>.

在EL中,还有&&运算符,也需要将其转义为&amp;&amp;,但是幸运的是,该运算符还有一个别名,即and运算符.

In EL, there is also the && operator which also needs to be escaped as &amp;&amp; as well, but fortunately there's an alias for this operator, the and operator.

这篇关于为什么在JSF中对与号(&)进行编码?有没有解决的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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