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

18,820

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

Share:
18,820

Related videos on Youtube

BalusC
Author by

BalusC

A deaf webapp specialist. Specialized in JSF. Currently working for Virtua Inc. Creator of OmniFaces. Committer of Mojarra. Author of The Definitive Guide to JSF. Want to get started with JSF the right way? Here's a curated overview of handpicked answers on JSF. You can hire me by sending a message at LinkedIn. You can donate me at PayPal. You can find here my latest JSF tutorial.

Updated on July 04, 2022

Comments

  • BalusC
    BalusC almost 2 years

    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?

    • Admin
      Admin over 10 years
      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-html5
  • hinneLinks
    hinneLinks over 8 years
    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?
  • BalusC
    BalusC over 8 years
    @hinneLinks: if content type is absent, browsers will indeed autoguess based on URL. Fortunately, JSF always autoincludes the content type.