如何在JSF中使用Internet Explorer条件注释?

17

这是我在Eclipse中的源代码文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>

<body>

</body>
</html>

当我在IE9中查看时,它呈现文本:

<!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->

如果我查看源代码,它会显示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--[if lt IE 9]&gt;

      &lt;script src=&quot;http://html5shim.googlecode.com/svn/trunk/html5.js&quot;&gt;&lt;/script&gt;

    &lt;![endif]-->
</head>

<body>


</body>
</html>

有什么原因导致Faces Servlet服务后源代码发生了变化吗?

3个回答

17

已知问题,JSF渲染会转义注释。您可以通过使用 <h:outputText escape="false"> 和 HTML 实体来解决它。您还可以使用 OmniFaces 的 <o:conditionalComment> 以更好的方式解决它。请参见展示站点:

<o:conditionalComment> 渲染条件注释。条件注释是 IE 特有的功能,它使开发者可以根据客户端是否使用IE及其版本来注释(或取消注释)HTML块。它们通常与 CSS 样式表结合使用,如下所示:

<!--[if lte IE 7]>
    <link rel="stylesheet" href="ie6-ie7.css" />
<![endif]-->

然而,Facelets会对注释内容进行HTML转义处理,导致无法使用。

<!--[if lte IE 7]&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;ie6-ie7.css&quot; /&gt;
&lt;![endif]-->

此外,如果将javax.faces.FACELETS_SKIP_COMMENTS上下文参数设置为true,则不会呈现任何内容。你需要使用一个丑陋的<h:outputText escape="false">来解决这个问题。

<h:outputText 
    value="&lt;!--[if lte IE 7]&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;ie6-ie7.css&quot; /&gt;&lt;![endif]--&gt;"
    escape="false" />

该组件旨在解决此问题。

<o:conditionalComment if="lte IE 7">
    <link rel="stylesheet" href="ie6-ie7.css" />
</o:conditionalComment>

请注意,您不能将此与<h:outputStylesheet>一起使用,因为它会被隐式重新定位为<h:head>的直接子节点。


9

这个有效:

<h:outputText escape="false" value="&lt;!--[if lt IE 9]&gt;   &lt;script src=&quot;http://html5shim.googlecode.com/svn/trunk/html5.js&quot;&gt;&lt;/script&gt;  &lt;![endif]--&gt;"></h:outputText>

1
你不必把脚本部分放在那里。你也可以把它分开并保留脚本标签以外的outputText。 - Christophe Roussy

2
我使用了f:verbatim,这样更加简洁明了。
例如:
<f:verbatim>
    <!--[if gte IE 8]>
       <script type="text/javascript">
        //<![CDATA[
        /** Javascript **/
        //]]>
        </script>
    <![endif]-->
</f:verbatim>

JSF核心标签参考

注意:自JSF 2.0(2009年12月)起,此标签已被弃用。它仅适用于JSP。


自 JSF 2.0(2009 年 12 月)起,此标记已被弃用。它仅适用于 JSP。 - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接