JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used
Asked Answered
L

1

18

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?

Leningrad answered 4/10, 2013 at 19:34 Comment(3)
Facelets is heavily based on XHTML. What's your specific question?Rathbun
Thanks, I can see that now! I wanted to know if there was some HTML5 verson of facelets. I thought the facelet tags were quite independent of the html itself and that 2.2 would enable HTML5 type facelets. Apparently, it's not that simple. This link explains how JSF 2.2 renders the facelets as HTML5, which is probably exactly what I want: jsflive.wordpress.com/2013/08/08/jsf22-html5Leningrad
When you go to a source code of rendered page it will have a HTML5 doctype and all it's new features seems to work just fine. Only Netbeans will not give you autocomplete insight on HTML5 tags & attributes.Hygiene
B
39

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".

Baldachin answered 4/10, 2013 at 22:48 Comment(2)
I wonder if its a problem, when the the URL is still something like server/app/login.xhtml, but the page itself is HTML5 and content-type is text/html? Or should the URL be .html instead of .xhtml?Lipps
@hinneLinks: if content type is absent, browsers will indeed autoguess based on URL. Fortunately, JSF always autoincludes the content type.Baldachin

© 2022 - 2024 — McMap. All rights reserved.