JavaServer Faces 2.2和HTML5支持,为什么还在使用XHTML [英] JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used

查看:129
本文介绍了JavaServer Faces 2.2和HTML5支持,为什么还在使用XHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了关于 JSF 2.2中的HTML5支持后,我惊讶地发现,在Netbeans中使用新的Web应用程序创建的模板文件中找到与以前的Facelets版本相同的XHTML文档类型。唯一的区别是HTML标签读入JSF 2.2:

 < html xmlns =http://www.w3 .org / 1999 / xhtml
xmlns:h =http://xmlns.jcp.org/jsf/html>

,而不是在旧的JSF版本中如下所示:

 < html xmlns =http://www.w3.org/1999/xhtml
xmlns:h =http://java.sun。 COM / JSF / HTML>

继续使用符合HTML5承诺的XHTML似乎有点不妥。如果我从已注册的库而不是服务器库中选择它,我只能选择使用2.2。这是否会影响框架版本应用于项目的方式?



Facelets太依赖XHTML来支持HTML5吗?

我们的XHTML wiki页面。仔细阅读。简而言之,Facelets绝对不关心生成的HTML输出中使用的doctype。您完全可以在Facelets模板中声明HTML5文档类型。



确实很遗憾Netbeans默认使用XHTML doctype准备文档,而现在推荐的文档是HTML5。我不做Netbeans,但在Eclipse中你可以轻松编辑这些模板甚至创建自己的模板。您只需用HTML5替换整个XHTML文档类型即可。您可以在IDE prefs中通过 Web»HTML文件»编辑器»模板查找/创建这些模板。

请注意JSF中的HTML5支持2.2与具体支持HTML5文档类型无关。相反,即使在使用传统JSP时,这在所有JSF版本上都受支持。 JSP和Facelets是一种视图技术,它允许您生成HTML输出,这可以很好地成为HTML5。这也是在下面密切相关的答案阐述:是否可以使用带有HTML 4/5的JSF + Facelets?



相反,JSF 2.2中的HTML5支持涵盖了定义自定义JSF组件属性并将自定义HTML元素转换为JSF组件。这在JSF 2.1和之前是不可能的。任何自定义的JSF组件属性(包括推荐HTML5的 data-xxx 属性)都被默认的JSF渲染器忽略。另请参阅以下相关答案:自定义HTML标记JSF不呈现属性。在JSF 2.2中,您可以通过新的 http://xmlns.jcp.org/jsf/passthrough 命名空间轻松指定自定义属性,如下所示:

 < html ... xmlns:a =http://xmlns.jcp.org/jsf/passthrough> 
...
< h:inputText ... a:autocorrect =off/>

这将在< h:inputText> 不支持的属性 autocorrect 实际上包含在生成的HTML输出中。请注意,我使用 a (attribute)而不是 p 的XML名称空间前缀,如 Java EE教程,否则它会与默认的XML命名空间冲突前缀 p PrimeFaces。



将自定义HTML元素(包括HTML5元素)转换为JSF组件需要指定 jsf 属性,如 jsf:id

 < html ... xmlns:jsf =http://xmlns.jcp.org/jsf> 
...
< header jsf:id =header> ...< / header>
< main jsf:id =main> ...< / main>
< footer jsf:id =footer> ...< / footer>

那些将被转化为 UIPanel (例如< h:panelGroup> )。是的,它们可以参考例如< f:ajax render>



换句话说,HTML5支持 自定义属性支持。


Having read about HTML5 support in JSF 2.2, I was surprised to find the same XHTML doctype as in previous Facelets versions in the template file created with a new web application in Netbeans. The only difference is that the HTML tag reads in JSF 2.2:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

, rather than as follows in older JSF versions:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

It seems a bit wrong to keep using XHTML with the promise of HTML5 in mind. I only have the option to use 2.2 if I choose it from "registered libraries" instead of "server library". Does this affect the way the framework version is applied to the project?

Is Facelets too much dependent on XHTML to support HTML5?

解决方案

It seems that you misunderstood the purpose of XHTML like as many people during the XHTML overhype a couple of years ago. Long story short: check our XHTML wiki page. Carefully read it. In a nutshell, Facelets absolutely doesn't care about the doctype being used in the generated HTML output. You can perfectly fine declare a HTML5 doctype in a Facelets template.

It's indeed unfortunate that Netbeans by default prepares the document with XHTML doctype while HTML5 is these days the recommended doctype. I don't do Netbeans, but in Eclipse you can easily edit those templates and even create your own. You can just replace the whole XHTML doctype by a HTML5 one. You can find/create those templates via Web » HTML Files » Editor » Templates in IDE prefs.

Please note that the HTML5 support in JSF 2.2 has got nothing to do with being able to support specifically the HTML5 doctype. On the contrary, this is supported on all JSF versions, even when legacy JSP is being used. JSP and Facelets are view technologies which allows you to generate HTML output, which can perfectly fine be HTML5 as good. This is also elaborated in the following closely related answer: Is it possible to use JSF+Facelets with HTML 4/5?

Instead, the HTML5 support in JSF 2.2 covers the possibility to define custom JSF component attributes and turning custom HTML elements into JSF components. This was not possible in JSF 2.1 and before. Any custom JSF component attributes (including the HTML5-recommended data-xxx attributes) were simply ignored by the default JSF renderers. See also the following related answer: Custom HTML tag attributes are not rendered by JSF. In JSF 2.2 you can easily specify custom attributes by the new http://xmlns.jcp.org/jsf/passthrough namespace as follows:

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<h:inputText ... a:autocorrect="off" />

This will end up in the by <h:inputText> unsupported attribute autocorrect to actually be included in the generated HTML output. Note that I use a XML namespace prefix of a ("attribute") instead of p as shown in the Java EE tutorial, as it would otherwise clash with default XML namespace prefix p of PrimeFaces.

Turning custom HTML elements (including HTML5 elements) into JSF components is a matter of specifying a jsf attribute such as jsf:id.

<html ... xmlns:jsf="http://xmlns.jcp.org/jsf">
...
<header jsf:id="header">...</header>
<main jsf:id="main">...</main>
<footer jsf:id="footer">...</footer>

Those will under the covers be turned into UIPanel (like as <h:panelGroup>). And yes, they are referencable in e.g. <f:ajax render>.

In other words, "HTML5 support" is just again another buzzword for "Custom attribute support".

这篇关于JavaServer Faces 2.2和HTML5支持,为什么还在使用XHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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