JSF 2如何根据浏览器版本设置html样式 [英] JSF 2 how to set html style depending on browser version

查看:138
本文介绍了JSF 2如何根据浏览器版本设置html样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JSF 2应用程序中执行的操作是根据浏览器版本设置适当的html样式,这里是示例代码:

 <! -  [if lt IE 7]> < html class =lt-ie9 lt-ie8 lt-ie7lang =en>百分比抑制率ENDIF]  -  GT!; 
<! - [if IE 7]> < html class =lt-ie9 lt-ie8lang =en>百分比抑制率ENDIF] - GT!;
<! - [if IE 8]> < html class =lt-ie9lang =en>百分比抑制率ENDIF] - GT!;
<! - [如果gt IE 8]><! - > <! - <![ENDIF] - GT;

在JSF 2中如何做到这一点?什么是最好的/最简单的方法?

我在Omnifaces( o:conditionalComment )中发现了一些有趣的事情,但它只允许整个css的条件加载,对我来说毫无用处......

解决方案

< o:conditionalComment> 仅适用于CSS文件 。这是一种误解。从技术角度讲,使用纯Facelets标签/组件来实现您的要求是不可能的,因为这会导致格式错误的XML语法,从而导致Facelets编译错误。

 < o:conditionalComment if =lt IE 7> 
< html lang =enclass =lt-ie9 lt-ie8 lt-ie7>
< / o:conditionalComment>
< o:conditionalComment if =IE 7>
< html lang =enclass =lt-ie9 lt-ie8>
< / o:conditionalComment>
< o:conditionalComment if =IE 8>
< html lang =enclass =lt-ie9>
< / o:conditionalComment>
< o:conditionalComment if =gt IE 8>
< h:outputText value =& lt;! - & gt; escape =false/>
< html lang =enclass =no-js>
< h:outputText value =& lt;! - escape =false/>
< / o:conditionalComment>
...
< / html>

这是不是 格式良好 XML。您知道,XML结束元素应该​​与XML start元素的范围相同。如果每一个< html> 在同一个父XML中有它自己的< / html> ,那么这将是有效的XML元件。但是这反过来又不是有效的HTML。



只要使用< h:outputText escape =false> 全部< html> 标签。不要忘记对结束< / html> 标签做同样的事情。

 < o:conditionalComment if =lt IE 7> 
< h:outputText value =& lt; html lang =& quot; en& quot; class =& quot; lt-ie9 lt-ie8 lt-ie7& gt; escape =false/>
< / o:conditionalComment>
< o:conditionalComment if =IE 7>
< h:outputText value =& lt; html lang =& quot; en& quot; class =& quot; lt-ie9 lt-ie8& quot;& gt; escape =false/>
< / o:conditionalComment>
< o:conditionalComment if =IE 8>
< h:outputText value =& lt; html lang =& quot; en& quot; class =& quot; lt-ie9& quot;& gt; escape =false/>
< / o:conditionalComment>
< o:conditionalComment if =gt IE 8>
< h:outputText value =& lt;! - & gt;& lt; html lang =& quot; en& quot; class =& quot; no-js& quot; & gt;& lt;! - escape =false/>
< / o:conditionalComment>
...
< h:outputText value =< / html& gt; escape =false/>

很尴尬,但 HTML5样板(这种方法起源于此)整体上也很尴尬,IMO。另外,您可以考虑创建一个自定义的< my:html> 组件自己生成所需的标记,以便您可以满足< my:html> ...< / my:html>


What i want to do in my JSF 2 application is to set proper html style depending on browser version, here is sample code:

<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <!--<![endif]-->

How to do that in JSF 2? What is the best/simplest way?

I found something interesting in Omnifaces (o:conditionalComment) but it only allows conditional loading of entire css which is useless for me...

解决方案

It's not true that the <o:conditionalComment> is for CSS files only. That's a misconception. It's technically simply not possible to achieve your requirement with pure Facelets tags/components as it would result in malformed XML syntax and thus a Facelets compile error.

<o:conditionalComment if="lt IE 7">
    <html lang="en" class="lt-ie9 lt-ie8 lt-ie7">
</o:conditionalComment>
<o:conditionalComment if="IE 7">
    <html lang="en" class="lt-ie9 lt-ie8">
</o:conditionalComment>
<o:conditionalComment if="IE 8">
    <html lang="en" class="lt-ie9">
</o:conditionalComment>
<o:conditionalComment if="gt IE 8">
    <h:outputText value="&lt;!--&gt;" escape="false" />
    <html lang="en" class="no-js">
    <h:outputText value="&lt;!--" escape="false" />
</o:conditionalComment>
...
</html>

This is not well formed XML. You know, the XML end element should be in the very same scope as XML start element. It would be valid XML if every <html> has its own </html> in the same parent XML element. But this is in turn not valid HTML.

Just use <h:outputText escape="false"> on all <html> tags. Don't forget to do the same for the closing </html> tag.

<o:conditionalComment if="lt IE 7">
    <h:outputText value="&lt;html lang=&quot;en&quot; class=&quot;lt-ie9 lt-ie8 lt-ie7&quot;&gt;" escape="false" />
</o:conditionalComment>
<o:conditionalComment if="IE 7">
    <h:outputText value="&lt;html lang=&quot;en&quot; class=&quot;lt-ie9 lt-ie8&quot;&gt;" escape="false" />
</o:conditionalComment>
<o:conditionalComment if="IE 8">
    <h:outputText value="&lt;html lang=&quot;en&quot; class=&quot;lt-ie9&quot;&gt;" escape="false" />
</o:conditionalComment>
<o:conditionalComment if="gt IE 8">
    <h:outputText value="&lt;!--&gt;&lt;html lang=&quot;en&quot; class=&quot;no-js&quot;&gt;&lt;!--" escape="false" />
</o:conditionalComment>
...
<h:outputText value="&lt;/html&gt;" escape="false" />

Pretty awkward, but the HTML5 boilerplate (where this approach originated) is at its whole own also awkward, IMO.

Alternatively, you could consider creating a custom <my:html> component yourself which generates the desired markup so that you can just suffice with <my:html>...</my:html>.

这篇关于JSF 2如何根据浏览器版本设置html样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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