Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML prolog / instruction not removed from XHTML output

I'm starting to learn JavaServer Faces (JSF). I'm using GlassFish 3+. I've just created a new JSF project in NetBeans and run the project. It worked fine, but upon examining the XHTML output, I noticed the XML declaration was left in. This messes up the DOCTYPE declaration (which is always supposed to be first in the document).

enter image description here

Is JSF supposed to remove the XML declaration, or is there something I've done wrong?

like image 288
Matt Larsen Avatar asked May 22 '12 08:05

Matt Larsen


2 Answers

Facelets will by default only remove it from compositions (include files and composite components) and tag files. It won't remove it from the master template. Just remove it yourself. You shouldn't be using the XML prolog at all when authoring HTML.

Whether the XML prolog will be removed from the master template is specified in appendix 1.1.1.1 of JSF 2.2 specification which describes the configuration of <facelets-processing> element in faces-config.xml. The XML prolog is described as "processing instructions". In the table, you'll see that it is only removed (consumed) when the template is processed as a XML or JSPX view.

1.1.1.1 The facelets-processing element

The <facelets-processing> element is used to affect the processing of Facelets VDL files. Therefore, this setting only applies to those requests that reach the Facelets ViewDeclarationLanguage implementation, as specified to the runtime via the javax.faces.FACELETS_VIEW_MAPPINGS and javax.faces.DEFAULT_SUFFIX <context-param> entries. The specification defines three processing modes for Facelets files: Facelets XHTML syntax, XML View syntax, and Facelets JSPX syntax. This last syntax is intended to ease the migration to Facelets for applications already using the JSP document syntax (also known as JSPX syntax). The affect on the processing of files in each of these three modes is specified in the following table.

Valid <process-as> values and their implications on the processing of Facelets.
-----------------------------------------------------------------------------------------
              <process-as>         <process-as>         <process-as>       <process-as>
              html5</process-as>   xhtml</process-as>   xml</process-as>   jspx</process-as>
              HTML 5 (default)     Facelets XHTML       XML View           Facelets JSPX
-----------------------------------------------------------------------------------------
XML Doctype   Simplified to        passed through       consumed           consumed
              <!DOCTYPE html>  

XML           passed through       passed through       consumed           consumed
declaration 

Processing    passed through       passed through       consumed           consumed
instructions

CDATA         passed through       passed through       consumed           consumed
section

Escaping of   escaped              escaped              escaped            not escaped
inline text    

XML           passed through       passed through       consumed           consumed
Comments 

In the preceding table, “passed through” means that the content is passed through unmodified to the user agent. “consumed” means the content is silently consumed on the server. Note that for CDATA sections, the content of the CDATA section itself is passed through, even if the start and end tags should be consumed. “escaped” means that sensivite content in the response is automatically escaped: & becomes &amp;, for example. “not escaped” means that such content is not escaped.

In other words, when you're authoring HTML5/XHTML, you have to remove it yourself. A better wording is actually: you shouldn't be including the XML prolog yourself in HTML5 and XHTML pages as that's not required; it's only required in XML and JSPX pages (and thus Facelets will automatically remove it).

See also:

  • JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used
  • Is it possible to use JSF+Facelets with HTML 4/5?

Unrelated to the concrete problem, you should be using <h:outputStylesheet> instead of <link rel="stylesheet"> to be independent from the request URL.

<h:outputStylesheet name="css/default.css" />
<h:outputStylesheet name="css/cssLayout.css" />

See also:

  • How to reference CSS / JS / image resource in Facelets template?
like image 179
BalusC Avatar answered Oct 28 '22 04:10

BalusC


In order to get the XML declaration to not appear on my rendered pages, I found that I could configure JSF to process my .xhtml files as XML. When processing in XML mode, the xml declaration would not pass through from my source files to the output. I have not yet noticed any other side effects to making this change. (But if I find any, I will follow up here.)

To make the config change, I added the following to my faces-config.xml:

<faces-config-extension>
    <facelets-processing>
        <file-extension>.xhtml</file-extension>
        <process-as>xml</process-as>
    </facelets-processing>  
</faces-config-extension>    

Hope this helps

like image 26
DWoldrich Avatar answered Oct 28 '22 05:10

DWoldrich