是否可以在 HTML 4/5 中使用 JSF+Facelets?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2935759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:15:41  来源:igfitidea点击:

Is it possible to use JSF+Facelets with HTML 4/5?

htmljsfjsf-2xhtmlfacelets

提问by Behrang Saeedzadeh

Facelets relies on XML namespaces to work with XHTML. How are HTML 4, and as far as I know, HTML 5 do not support namespaces. Also HTML 5 has some new elements that are not available in XHTML. Even HTML 4 and XHTML have some differences regarding elements and attributes they support.

Facelets 依赖于 XML 名称空间来处理 XHTML。HTML 4 怎么样,据我所知,HTML 5 不支持命名空间。HTML 5 还有一些 XHTML 中没有的新元素。甚至 HTML 4 和 XHTML 在它们支持的元素和属性方面也存在一些差异。

The question is: Is it possible to render HTML 4/5 documents using Facelets? If so, how?

问题是:是否可以使用 Facelets 呈现 HTML 4/5 文档?如果是这样,如何?

回答by BalusC

Since Facelets is a XML based view technology which eats and emits in essence XML markup, you cannot use it with a HTML4 doctype. The HTML4 doctype describes several elements which cannot be self-closing, like <link>, <meta>, <br>and <hr>. However, with XML you're forced to close them like <link/>, <meta/>, etc. So using a HTML4 doctype is absolutely not an option for Facelets (that is, when you respect the standards and/or fear the w3 validator, it will however work perfectly on the most if not all webbrowsers).

由于 Facelets 是一种基于 XML 的视图技术,它本质上吃和发出 XML 标记,因此您不能将它与 HTML4 文档类型一起使用。所述HTML4 DOCTYPE描述了几种元件,其不能自封闭,如<link><meta><br><hr>。但是,对于 XML,您不得不像<link/>,<meta/>等那样关闭它们。因此,使用 HTML4 doctype 绝对不是 Facelets 的选项(也就是说,当您尊重标准和/或害怕 w3 验证器时,它会完美地工作最多(如果不是全部)网络浏览器)。

HTML5, on the other hand, allows XML markup. This is specified in chapter 3.2.2 - Elements:

另一方面,HTML5 允许 XML 标记。这在第 3.2.2 章 - 元素中指定:

Example:

<link type="text/css" href="style.css"/>

Authors may optionally choose to use this same syntax for void elements in the HTML syntax as well. Some authors also choose to include whitespace before the slash, however this is not necessary. (Using whitespace in that fashion is a convention inherited from the compatibility guidelines in XHTML 1.0, Appendix C.)

例子:

<link type="text/css" href="style.css"/>

作者也可以选择对 HTML 语法中的 void 元素使用相同的语法。一些作者还选择在斜杠前包含空格,但这不是必需的。(以这种方式使用空格是从 XHTML 1.0 附录 C 中的兼容性指南继承而来的约定。)

I myself use <!DOCTYPE html>all the way, also with JSF/Facelets, even without a <?xml?>declaration in top of the page. It works perfectly in all browsers. With a XHTML doctype you should as per the specification be using a Content-Typeof application/xhtml+xmlwhich would only make MSIE to choke (it doesn't understand it). And since that's still one of the most widely used browsers... Replacing the XHTML content type by text/htmlis considered harmful, you also don't want to do this.

我自己一直使用<!DOCTYPE html>,也使用 JSF/Facelets,即使<?xml?>在页面顶部没有声明。它在所有浏览器中都能完美运行。随着XHTML文档类型,你应该按照规范来使用Content-Typeapplication/xhtml+xml,因为这只会令MSIE呛(不明白)。并且由于它仍然是最广泛使用的浏览器之一...替换 XHTML 内容类型text/html认为是有害的,您也不想这样做。

As per your arguments:

根据你的论点:

HTML 5 do not support namespaces.

HTML 5 不支持命名空间。

This doesn't matter. The namespaces are only of interest for the XML based server side view technology (like as Facelets) which in turn can generate pure HTML with those tags. The following example is legitimately valid for Facelets:

这没关系。命名空间只对基于 XML 的服务器端视图技术(如 Facelets)感兴趣,后者又可以生成带有这些标签的纯 HTML。以下示例对 Facelets 合法有效:

<!DOCTYPE html>
<html lang="en"
    xmlns:f="http://xmlns.jcp.org/jsf/core" 
    xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Title</title>
    </h:head>
    <h:body>
        <h:outputText value="#{bean.text}" />
    </h:body>
</html>

This renders legitimately valid HTML5 (for the client side):

这将呈现合法有效的 HTML5(对于客户端):

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
    </head>
    <body>
        Some text
    </body>
</html>

You see, Facelets already removes the XHTML declarations since they have no meaning in the client side.

您会看到,Facelets 已经删除了 XHTML 声明,因为它们在客户端没有任何意义。

And,

和,

Also HTML 5 has some new elements that are not available in XHTML

HTML 5 还有一些 XHTML 中没有的新元素

this make also no sense. It's all about the generated output. Which can be HTML5 as good. Your only problem may be the browser support and the availability of 3rd party JSF components which renders HTML5 specific elements. Since JSF 2.2, it's possible to use the new passthrough elementsfeature to turn custom elements into a JSF component. Simply give the HTML5 element a jsf:idattribute. It'll transparently internally be interpreted as a UIPanelinstance in the JSF component tree (like <h:panelGroup>).

这也没有任何意义。这都是关于生成的输出。哪个可以和 HTML5 一样好。您唯一的问题可能是浏览器支持和呈现 HTML5 特定元素的 3rd 方 JSF 组件的可用性。从 JSF 2.2 开始,可以使用新的传递元素功能将自定义元素转换为 JSF 组件。只需给 HTML5 元素一个jsf:id属性。它将在内部透明地解释为UIPanelJSF 组件树中的一个实例(如<h:panelGroup>)。

<!DOCTYPE html>
<html lang="en"
    xmlns:jsf="http://xmlns.jcp.org/jsf"
    xmlns:f="http://xmlns.jcp.org/jsf/core" 
    xmlns:h="http://xmlns.jcp.org/jsf/html"
>
    <h:head>
        <title>Title</title>
    </h:head>
    <h:body>
        <header jsf:id="header">Header</header>
        <nav jsf:id="nav">Nav</nav>
        <main jsf:id="main">Main</main>
        <footer jsf:id="footer">Footer</footer>
    </h:body>
</html>

You can even reference it from ajax as in <f:ajax render="main">.

您甚至可以从 ajax 中引用它,如<f:ajax render="main">.

Actually, XHTML is overhyped. Its sole intent is to ease HTML development using XML based toolswhich can manipulate/transform/generate HTML pages on the server side (like as Facelets). But some starters also use it without using any XML tool and output it plain as-is, because it's "so cool" -for some unclear reason.

实际上,XHTML 被夸大了。它的唯一目的是使用基于 XML 的工具简化 HTML 开发,这些工具可以在服务器端操作/转换/生成 HTML 页面(如 Facelets)。但是一些初学者也使用它而不使用任何 XML 工具并按原样输出它,因为它“太酷了” - 原因不明。

Don't get me wrong. XHTML is greatas server side view technology. But simply not as client side markup technology. It has utterly no value at the client side.

不要误会我的意思。XHTML 非常适合作为服务器端视图技术。但根本就不能作为客户端标记技术。它在客户端完全没有价值。

See also:

也可以看看:

回答by Vetle

On a related note, check out this IBM developerWorks article: JSF 2 fu: HTML5 composite components, Part 1

在相关说明中,查看这篇 IBM developerWorks 文章:JSF 2 fu:HTML5 复合组件,第 1 部分

回答by ogok

MyFaces has an extension for html5. Try this http://myfaces.apache.org/html5/

MyFaces 有一个 html5 扩展。试试这个http://myfaces.apache.org/html5/

回答by PageFault

I've read, that this should be possible, but I did not do it myself, yet. Maybe you should just use HTML 5 inside the xHTML wrapper code. I will see, if I can find the source of information I've again.

我读过,这应该是可能的,但我自己还没有做到。也许您应该只在 xHTML 包装器代码中使用 HTML 5。我会看看,如果我能再次找到信息的来源。

[EDIT] Seems like, there has been some work at MyFaces to support HTML5 rendering during Google's summer of code. I don't know if it should be used in a productive way, yet.

[编辑] 似乎,在 Google 的代码夏季期间,MyFaces 已经做了一些工作来支持 HTML5 渲染。我不知道它是否应该以一种富有成效的方式使用。

Please give us a feedback, if you get it to work. [/EDIT]

请给我们一个反馈,如果你得到它的工作。[/编辑]

回答by Martijn Verburg

http://wiki.whatwg.org/wiki/HTML_vs._XHTMLhas some useful information on how namespaces can be used in HTML5 to assist migration from XHTML. Perhaps you can try applying the namespace as it suggests and see what occurs?

http://wiki.whatwg.org/wiki/HTML_vs._XHTML有一些关于如何在 HTML5 中使用命名空间来帮助从 XHTML 迁移的有用信息。也许您可以按照它的建议尝试应用命名空间,看看会发生什么?