XML or text declaration not at start of entity

10,744

XML or text declaration not at start of entity

This error suggests that you have dangling <?xml ... ?> declarations at wrong places of the generated HTML output. This is invalid, there should be one at top of the document, or preferably, no one at all (otherwise MSIE will potentially go havoc). Check your Facelets include templates and tag files and make sure that they are all wrapped inside an <ui:composition>.

So, a fictive include.xhtml must look like this:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h2>Include page</h2>
    <p>Include page blah blah lorem ipsum</p>
</ui:composition>

and thus not this:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <?xml version="1.0" encoding="UTF-8"?>
    <h2>Include page</h2>
    <p>Include page blah blah lorem ipsum</p>
</ui:composition>

or even

<?xml version="1.0" encoding="UTF-8"?>
<x:anotherComponent
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h2>Include page</h2>
    <p>Include page blah blah lorem ipsum</p>
</x:anotherComponent>

It's by the way perfectly fine for Facelets to omit the XML declaration altogether.

See also:

Share:
10,744
Chris
Author by

Chris

Updated on June 05, 2022

Comments

  • Chris
    Chris almost 2 years

    Sorry being a bit daft. But I come to understand (my lack of knowledge) that it's not possible to forward to another page, if you want use RichFaces components on that page.

    This is some of the problems that I am experiance when forward to a page with RichFaces components

    • If i forward to a page with a form, there is some of the hundreds of included JavaScrips that is interpreted as a bad formatted XML tag.
    • If I use nested tables, the tables loose the CSS file and look like normal JSF 2.0 dataTables.
    • When forwarded to page with only a tabPanel as in the demo TabPanel - Show Case the tab panels get messed up and become not usable (see image beelow).

    I don't need to forward to pages with RichFaces components, but it would be nice to have that option. Probably I have misunderstood something crucial on how to use RichFaces.

    Just for your information, I've created a totally new web project in NetBeans 7.0.1 and made two pages. Via a4j:commandLink I forward from the first page to the second one that have a Tab Panel. The rendering get messed up and the panel becomes unusable. Except including the libs and tags necessary for RichFaces, the new project is totally clean of setup parameter in the web.xml and rich-faces.xml.

    What am I missing when I forward to a page with RichFaces components?

    PS. If there is a patter to follow, that would help a lot on how to make page forwarding work with RichFaces.

    Greetings Chris.

    This is the error firebug reports (after the forward is invoked)

    XML or text declaration not at start of entity
    http://localhost:8080/humis/faces/app_user/projectHome.xhtml
    Line 7
    

    Firebug report these status for the page

    • 200 OK
    • 304 Not Modified

    It is something in the header and in the 20-30 scripts included. Don't know how to include the long html list here. The request it self seams ok, but RichFaces generated something that I can control when doing a forward to the page.

    The masterLayout file;

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
        <h:outputStylesheet name="css/default.css"/>
        <h:outputStylesheet name="css/cssLayout.css"/>
    
        <title>
            <h:outputText value="Partner Tapestry - " /> <ui:insert name="title">Browser Title</ui:insert>
        </title>
    </h:head>
    
    <h:body>
        <div id="top" >
            <ui:insert name="top">Top Default</ui:insert>
        </div>
    
        <div id="messages">
            <rich:messages id="messagePanel" ajaxRendered="true"/>
        </div>
    
        <div id="content">
            <ui:insert name="content">Content Default</ui:insert>
        </div>
    </h:body>
    

    File using the template

    <ui:composition template="/resources/masterLayout.xhtml" 
                    xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:ui="http://java.sun.com/jsf/facelets"
                    xmlns:h="http://java.sun.com/jsf/html"
                    xmlns:f="http://java.sun.com/jsf/core"
                    xmlns:rich="http://richfaces.org/rich"
                    xmlns:a4j="http://richfaces.org/a4j">
    
        <ui:define name="title">
            <h:outputText value="#{bundle.EditActivityTitle}"></h:outputText>
        </ui:define>
    
        <ui:define name="top">
            <ui:include src="#{userController.roleMenuPath}"/>
        </ui:define>
    
        <ui:define name="content">
    
            <rich:panel header="Edit Activity" styleClass="center_content">
                <h:form id="activity_edit_form">
                ...
                </h:form>
            </rich:panel>
        </ui:define>
    </ui:composition>
    

    I am using:

    • RichFaces 4.0-final
    • Glassfish 3.1.1
    • I don't use maven and artefact for the library

    enter image description here

  • Chris
    Chris over 12 years
    I'll go though and see if there's tag outside the composite tags. Shouldn't the XML error also appear when I do and redirect ?
  • BalusC
    BalusC over 12 years
    The browser behaviour of interpreting this malformed markup is undetermined and specific to browser itself. Check the generated HTML output and look for dangling <?xml ... ?> declarations. Its location should hint enough where they originated in the JSF source.
  • Chris
    Chris over 12 years
    I included the masterLayout file. But the html part of the page didn't get pasted in.
  • BalusC
    BalusC over 12 years
    The problem is in the file which is using the master template. You need <ui:composition template="masterLayout.xhtml"> without any <?xml ... ?> declaration inside.
  • BalusC
    BalusC over 12 years
    Then there's another template in the path as specified by <ui:include src="#{userController.roleMenuPath}"/> which uses XML declaration at the wrong place or is missing a <ui:composition>.
  • Chris
    Chris over 12 years
    Remove also the *<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 .. * in my file included in my div=top. Now it seams that I have to submit twice for the actin to affect. But at least I got ride of the problem. Thanks BalusC, always spot on.
  • BalusC
    BalusC over 12 years
    The doctype declaration should go in master template alone and definitely not in an include/tag/composite file. It messes everything up. There's supposed to be only one in top of the page. As to the double submit problem, this can happen when you submit a form which re-renders another form. You should put its contents in a wrapper and re-render it instead.
  • Chris
    Chris over 12 years
    Yes, you get weird problem that it's hard to find the reason for. Specially when you get the templates and things from Netbeans to start with.
  • BalusC
    BalusC over 12 years
    I don't use it (I've always thrown it away after some time of trying, every few years again), so I can't explain what Netbeans was thinking/figuring out for you.