Krzysztof
Krzysztof

Reputation: 2082

Facelet template renders two HTML tags

I am creating my first facelets/JSF application. In my first page I added a facelet template:

<?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html 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"> 
<f:view>
  <h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Insert title here</title>
  </h:head>
  <body>
    <ui:insert name='top'>
      <ui:include src="/templates/template_a.xhtml"></ui:include>
</ui:insert>
  </body>
</f:view>
</html>

This is my template page:

  <?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html 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">
  <h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Insert title here</title>
  </h:head>
  <body>
    <ui:composition template="/inwert_a.xhtml">
      <ui:define name="top">
      <h2>naglowek</h2>
    </ui:define>
  </ui:composition>
</body> 
</html>

But when I look at page source in my web browser I see this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Insert title here</title>
  </head>
  <body>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Insert title here</title>
      </head>
      <body>
        <h2>naglowek</h2>
      </body>
    </html>
  </body>
</html>

Why does JSF create a body and other HTML tags twice when I am using <ui:composition>?


After update my code looks that-main page:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html 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"> 

 <h:head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
  <title>Insert title here</title> 
 </h:head> 
 <h:body>   
    <f:subview id="a">
        <ui:insert name="top">
           <ui:include src="aaa.xhtml" ></ui:include> 
        </ui:insert>
     </f:subview>
 </h:body> 
 </html>

template content:

<ui:composition 
 xmlns:ui="http://java.sun.com/jsf/facelets" 
 xmlns:h="http://java.sun.com/jsf/html"
 template="/mainpage.xhtml"
> 
  <ui:define name="top">
       <h2> aaaaaaaaa </h2>
  </ui:define>  

 </ui:composition>

this is source view:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org  
 /TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

 <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Insert title here</title></head>
 <body>

 <?xml version="1.0" encoding="ISO-8859-1" ?>     
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
 /TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Insert title here</title>

</head>
 <body>
    <h2>aaaaaaaaa</h2>
 </body> 
</html>
</body> 
</html>

Upvotes: 2

Views: 2286

Answers (1)

BalusC
BalusC

Reputation: 1108722

You're basically using templating the wrong way. You're basically using them "the other way round" from how it's supposed to be done. The page which you called "my template page" should be opened by its URL in the browser, not the page which you called "a facelet template". Best to prevent this mistake in the future is to store pages which you specify in <ui:composition template> (and <ui:include src>) inside /WEB-INF folder, so that they can never be opened directly by accident (or by hackers).

Here's a kickoff example which should work for your case:

/WEB-INF/templates/template.xhtml:

<!DOCTYPE html>
<html lang="en"
    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">
    <h:head>
        <title><ui:insert name="title">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <ui:insert name="top">
            <ui:include src="/WEB-INF/templates/top.xhtml">
        </ui:insert>
    </h:body>
</html>

/WEB-INF/templates/top.xhtml:

<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">
    <p>Some default content for top</p>
</ui:composition>

/page.xhtml

<ui:composition template="/WEB-INF/templates/template.xhtml"
    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">

    <ui:define name="title">
        New page title here
    </ui:define>

    <ui:define name="top">
        <h2>naglowek</h2>
    </ui:define>
</ui:composition>

Now, you should in your browser navigate to /page.xhtml (and thus not the template file).

See also:

Upvotes: 7

Related Questions