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

DD.*_*DD. 17 jsf internet-explorer facelets conditional-comments jsf-2

这是我在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>
Run Code Online (Sandbox Code Playgroud)

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

<!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->
Run Code Online (Sandbox Code Playgroud)

如果我查看来源它说:

<!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>
Run Code Online (Sandbox Code Playgroud)

Faces Servlet服务后源已更改的任何原因?

SJu*_*n76 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]-->
Run Code Online (Sandbox Code Playgroud)

但是,Facelets会将注释的内容呈现为HTML转义,从而使其无法使用.

<!--[if lte IE 7]&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;ie6-ie7.css&quot; /&gt;
&lt;![endif]-->
Run Code Online (Sandbox Code Playgroud)

此外,如果将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" />
Run Code Online (Sandbox Code Playgroud)

该组件旨在解决此问题.

<o:conditionalComment if="lte IE 7">
    <link rel="stylesheet" href="ie6-ie7.css" />
</o:conditionalComment>
Run Code Online (Sandbox Code Playgroud)

请注意,您不能使用它,<h:outputStylesheet>因为它将隐式地重新定位为直接子项<h:head>.


DD.*_*DD. 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>
Run Code Online (Sandbox Code Playgroud)

  • 丑陋,如同地狱,但它的作用,就像一个魅力.谢谢! (2认同)