JSF ui:Composition component does not work

Asked

Viewed 689 times

0

I’m creating an example template but when the page opens it goes blank, it follows the template and page that tries to pull up the template:

master-template.xhtml:

 <?xml version="1.0" encoding="UTF-8" ?>
 <!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="titulo">Sistema de livraria on-line</ui:insert></title>
<h:outputStylesheet library="css" name="sistema.css" />
</h:head>
<h:body>
<div id="conteudo">
    <ui:insert name="corpo" />
</div>
</h:body>
<p:separator style="margin-top: 20px" />
<footer> Exemplo de Template - <a href="#" target="_blank">www.exemplo.com.br</a>

</html>

example.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/template/master-template.xhtml ">

<ui:define name="conteudo">Exemplo 2</ui:define>

</ui:composition>

And when the page opens it is only displaying "Exemplo2" and looking at the source code it generated in the browser it looks like this:

<html>
<head></head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:f="http://xmlns.jcp.org/jsf/core" template="/WEB-INF/template/master-template.xhtml ">
<ui:define name="conteudo">Exemplo 2</ui:define>
</ui:composition>
</body>
</html>

1 answer

3

In his template main, you declared an HTML element div#conteudo and defined that the JSF will render an area identified by name corpo inside this element, as it can be checked in the file master-template.xhtml:

<div id="conteudo">
    <ui:insert name="corpo" />
</div>

However, when using the template, you make the definition (ui:defines) of an area identified as conteudo, as can be checked in your file example.xhtml:

<ui:define name="conteudo">Exemplo 2</ui:define>

When rendering your page, the JSF received an area identified by the name conteudo but received no instruction on how to render the same.

I recommend reading the following material, which makes this explanation very clear: JSF 2 Templating with Facelets example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.