With Facelets this became easy, note in my case is an example you need, below we have a standard template for Cruds:
<?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">
<ui:composition template="/layout/template.xhtml">
<ui:define name="content">
<h:form id="pesquisa" rendered="#{!controller.mudarPagina}">
<p:messages id="messages" showDetail="true" autoUpdate="false" closable="false" globalOnly="true" />
<p:panel header="#{tituloPagina}">
<ui:insert name="camposPesquisa" />
</p:panel>
<p:toolbar>
<f:facet name="left">
<h:panelGrid columns="2">
<p:commandButton action="#{controller.pesquisar}" value="#{msg['pesquisar']}" ajax="false" update="resultadoPesquisa" icon="ui-icon-search"/>
<p:commandButton action="#{controller.novo}" value="#{msg['novo']}" ajax="false" update="cadastro" icon="ui-icon-plusthick"/>
</h:panelGrid>
</f:facet>
</p:toolbar>
<p:spacer height="0px" />
<p:dataTable id="resultadoPesquisa" var="bean" value="#{controller.lista}" paginator="true" rows="10"
rendered="#{!empty controller.lista}" paginatorPosition="top">
<p:column styleClass="botoesGrid">
<p:commandButton icon="ui-icon-pencil" action="#{controller.editar(bean.id)}" update="cadastro" ajax="false" title="#{msg['editar']}"/>
<p:commandButton icon="ui-icon-trash" action="#{controller.excluir(bean)}" process="@this" update="pesquisa" title="#{msg['remover']}">
<p:confirm header="#{msg['cabecalho.apagar.registro']}" message="#{msg['apagar.registro']}" icon="ui-icon-alert" />
</p:commandButton>
<p:confirmDialog global="true" showEffect="exploud" hideEffect="fade">
<p:commandButton value="#{msg['sim']}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="#{msg['nao']}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</p:column>
<ui:insert name="resultado" />
<f:facet name="footer">
<h:outputText value="#{msg['total.registros']}: #{controller.lista.size()}" />
</f:facet>
</p:dataTable>
</h:form>
<h:form id="cadastroEdicao" rendered="#{controller.mudarPagina}">
<p:panel header="#{empty idEntidade ? msg['cadastrando'] : msg['atualizando']} #{tituloPagina}">
<ui:insert name="cadastroEdicao" />
</p:panel>
<p:toolbar>
<f:facet name="left">
<h:panelGrid columns="4">
<p:commandButton action="#{controller.cancelar}" value="#{msg['voltar']}"
update="cadastro,pesquisa,:pesquisa:resultadoPesquisa" immediate="true" ajax="false" icon="ui-icon-arrowreturnthick-1-w"/>
<p:commandButton action="#{controller.gravar}" value="#{msg['salvar']}"
update="cadastro,pequisa,:pesquisa:resultadoPesquisa" ajax="false" icon="ui-icon-disk"/>
<p:commandButton actionListener="#{controller.novo}" value="#{msg['novo']}" ajax="false" rendered="#{idEntidade != null}" />
<p:commandButton action="#{controller.salvarENovo}" value="#{msg['salvar.novo']}" ajax="false" update="cadastro" icon="ui-icon-plusthick"/>
</h:panelGrid>
</f:facet>
</p:toolbar>
</h:form>
</ui:define>
</ui:composition>
</html>
Note that I have some variables in the middle, ex: controller
, tituloPagina
, idEntidade
, etc..
Now I will use this Templete on a page:
<?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">
<ui:composition template="/layout/template-crud.xhtml">
<ui:param name="controller" value="#{natureController}" />
<ui:param name="idEntidade" value="#{natureController.nature.id}" />
<ui:param name="tituloPagina" value="#{msg['nature']}" />
<!-- TELA DE PESQUISA -->
<ui:define name="camposPesquisa">
<h:panelGrid id="camposPesquisa" columns="2">
<h:outputLabel value="#{msg['nome']}: " />
<p:inputText />
</h:panelGrid>
</ui:define>
<!-- RESULTADO DA PESQUISA -->
<ui:define name="resultado">
<p:column headerText="#{msg['nome']}" sortBy="#{bean.nome}" style="width:12%;">
<h:outputText value="#{bean.nome}" />
</p:column>
<p:column headerText="#{msg['aumenta']}" sortBy="#{bean.aumenta}">
<h:outputText id="diminui" value="#{bean.aumenta}" style="color: #{bean.aumenta == 'Neutro' ? 'none' : 'red'}" />
</p:column>
<p:column headerText="#{msg['diminui']}" sortBy="#{bean.diminui}">
<h:outputText value="#{bean.diminui}" style="color: #{bean.diminui == 'Neutro' ? 'none' : 'blue'}" />
</p:column>
<p:column headerText="#{msg['sabor.favorito']}" sortBy="#{bean.saborFavorito}">
<h:outputText value="#{bean.saborFavorito}" />
</p:column>
<p:column headerText="#{msg['sabor.nao.favorito']}" sortBy="#{bean.saborQueNaoGosta}">
<h:outputText value="#{bean.saborQueNaoGosta}" />
</p:column>
</ui:define>
<!-- TELA DE CADASTRO E EDIÇÃO -->
<ui:define name="cadastroEdicao">
<h:panelGrid id="informacoesCadastro" columns="3">
<h:outputLabel value="#{msg['nome']}" />
<p:inputText id="nome" value="#{natureController.nature.nome}" size="20" />
<p:message for="nome" />
<h:outputLabel value="#{msg['aumenta']}: " rendered="true" />
<p:selectOneMenu id="aumenta" value="#{natureController.nature.aumenta}" effect="clip">
<f:selectItem itemLabel="" />
<f:selectItem itemLabel="#{msg['neutro']}" itemValue="Neutro" />
<f:selectItems value="#{enumHelper.listarStatsBase()}" var="bean" itemLabel="#{bean.nome}" itemValue="#{bean.nome}" />
</p:selectOneMenu>
<p:message for="aumenta" />
<h:outputLabel value="#{msg['diminui']}: " />
<p:selectOneMenu id="diminui" value="#{natureController.nature.diminui}" effect="clip">
<f:selectItem itemLabel="" />
<f:selectItem itemLabel="#{msg['neutro']}" itemValue="Neutro" />
<f:selectItems value="#{enumHelper.listarStatsBase()}" var="bean" itemLabel="#{bean.nome}" itemValue="#{bean.nome}" />
</p:selectOneMenu>
<p:message for="diminui" />
</h:panelGrid>
</ui:define>
</ui:composition>
</html>
Note that I pass the parameters at the top of the page:
<ui:param name="controller" value="#{natureController}" />
<ui:param name="idEntidade" value="#{natureController.nature.id}" />
<ui:param name="tituloPagina" value="#{msg['nature']}" />
Exists through the Composite Components feature of JSF2 http://www.devmedia.com.br/jsf-composite-aplicando-reusabilidade-de-codigo/30329 http://stackoverflow.com/tags/composite-component/info http://www.oracle.com/technetwork/pt/articles/java/componentes-java-server-faces-1932583-ptb.html
– Helis