自定义JSF组件:使用“startElement”与“脚本”结果发表评论 [英] Custom JSF component: Using "startElement" with "script" results in a comment

查看:172
本文介绍了自定义JSF组件:使用“startElement”与“脚本”结果发表评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在渲染自定义的JSF组件。在方法 encodeBegin 中,我想包括一些java脚本。

I'm rendering a custom JSF component. In the method encodeBegin I want to include some java script.

public void encodeBegin(FacesContext context) throws IOException {
   ResponseWriter writer = context.getResponseWriter();
   writer.startElement("script", this);
   writer.writeAttribute("type", "text/javascript", null);
   writer.writeText("var width=400",null);
   writer.endElement("script");
}

在渲染组件时,脚本标记的内容被注释掉。 p>

When rendering the component the content of the script tag is commented out.

<script type="text/javascript"><!--
var width=400;
//--></script>

任何人都可以解释为什么会显示此评论,以及如何解决?

Can anybody explain why this comment appears and how I get rid of it?

提前感谢!

推荐答案

这是特定于MyFaces实现,而不是JSF规范。

This is specific to the MyFaces implementation, not to JSF specification. The Mojarra implementation doesn't do that.

这种将JavaScript正文放在HTML注释中的方法基本上是HTML的史前回溯,当浏览器存在时,不会支持< script> 元素。这些HTML注释基本上隐藏了JavaScript内容,以防止那些古老的HTML解析器解释并显示JavaScript代码纯文本

This approach of putting JavaScript body in a HTML comment is basically a leftover of the HTML prehistory back when browsers existed which doesn't support <script> elements. Those HTML comments basically hides the JavaScript content to prevent those ancient HTML parsers from interpreting and displaying JavaScript code as plain text.

href =https://developer.mozilla.org/en/docs/Writing_JavaScript_for_XHTML =nofollow> Mozilla开发人员网络 - 为XHTML编写JavaScript :

See also Mozilla Developer Network - Writing JavaScript for XHTML:


这是HTML中的常见做法,用于从不支持JS的浏览器中隐藏脚本。在XML的时代,意见是他们的目的:意见。在处理文件之前,所有注释将从文档中删除,因此将脚本包含在文档中就像在Piranha池中抛弃午餐。此外,真的没有意义,你的脚本注释 - 没有浏览器写在过去十年将显示您的代码在页面上。

This was common practice in HTML, to hide the scripts from browsers not capable of JS. In the age of XML comments are what they were intended: comments. Before processing the file, all comments will be stripped from the document, so enclosing your script in them is like throwing your lunch in a Piranha pool. Moreover, there's really no point to commenting out your scripts -- no browser written in the last ten years will display your code on the page.

注意最后一句话,这是非常真实的,在过去十年中没有一个浏览器开发了。 MyFaces显然有点夸张,假设现在仍然使用这样的史前浏览器。

Note the last sentence, this is very true, no single browser developed in the last decade would do that anymore. MyFaces is apparently a bit overzealously assuming that one would still be using such a prehistoric browser nowadays.

这篇关于自定义JSF组件:使用“startElement”与“脚本”结果发表评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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