Olá, caros.
A dúvida é algo que era para parecer simples.
Implementei uma paginação à partir do blog de Marcus mazzo.
Funcionou muito bem, porém, ao executar o componente pela primeira vez os dados são carregados e há de fato uma paginação, porém, ao clicar sobre o link da página pela segunda vez, o layout simplesmente se perde, como se não houvesse Css definido, as cores somes e as divisões da tabela, como se não houvesse implementado, de fato uma suite de componentes.
Classe para paginar:
package br.com.agets.paginator;
import java.io.Serializable;
import java.util.List;
import javax.faces.model.DataModel;
@SuppressWarnings("rawtypes")
public class PagedDataModel extends DataModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int rowIndex = -1;
private int totalNumRows;
private int pageSize;
private List<?> list;
public PagedDataModel() {
super();
}
public PagedDataModel(List<?> list, int totalNumRows) {
super();
setWrappedData(list);
this.totalNumRows = totalNumRows;
this.pageSize = list.size();
}
public PagedDataModel(List<?> list, int totalNumRows, int pageSize) {
super();
setWrappedData(list);
this.totalNumRows = totalNumRows;
this.pageSize = pageSize;
}
@Override
public boolean isRowAvailable() {
if (list == null)
return false;
int rowIndex = getRowIndex();
if (rowIndex >= 0 && rowIndex < list.size())
return true;
else
return false;
}
@Override
public int getRowCount() {
return totalNumRows;
}
@Override
public Object getRowData() {
if (list == null)
return null;
else if (!isRowAvailable())
throw new IllegalArgumentException();
else {
int dataIndex = getRowIndex();
return list.get(dataIndex);
}
}
@Override
public int getRowIndex() {
try {
return (rowIndex % pageSize);
} catch (ArithmeticException e) {
return 0;
}
}
@Override
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
@Override
public Object getWrappedData() {
return list;
}
@Override
public void setWrappedData(Object list) {
this.list = (List<?>) list;
}
}
Métodos usados para paginação com Hibernate.
@SuppressWarnings("unchecked")
public List<T> listarTodos(Integer startPage, Integer maxPage, List<Criterion> restricoes, List<Projection> projecoes, List<Order> ordenacao) {
Criteria criteria = this.session.createCriteria(this.persistentClass);
List<T> list = new ArrayList<T>();
if (restricoes != null) {
for (Criterion r : restricoes) {
criteria.add(r);
}
}
if (projecoes != null) {
for (Projection p : projecoes) {
criteria.setProjection(p);
}
}
if (ordenacao != null) {
for (Order order : ordenacao) {
criteria.addOrder(order);
}
}
criteria.setFirstResult(startPage);
criteria.setMaxResults(maxPage);
list = criteria.list();
for (T t : list) {
for (Method method : t.getClass().getMethods()) {
if (method.getName().indexOf("get") > -1) {
Object obj;
try {
obj = method.invoke(t);
if (obj instanceof java.util.Collection) {
for (T lista : (List<T>) obj) {
Hibernate.initialize(lista);
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
return list;
}
public Integer count(List<Criterion> restricoes) {
Number size = 0;
Criteria c = this.session.createCriteria(this.persistentClass);
if (restricoes != null) {
for (Criterion criterion : restricoes) {
c.add(criterion);
}
}
size = ((Long) c.setProjection(Projections.rowCount()).uniqueResult());
return size.intValue();
}
Implementação da paginação mo MBean
private DataModel dataModel;
private UIDataTable uiDataTable;
//GETS/SETS destes valores acima.
@SuppressWarnings("rawtypes")
public DataModel getDataModel() {
int totalListSize = new AtendimentoService().count();
List<Atendimento> pagedList = new AtendimentoService().listarTodos(getUiDataTable().getFirst(), getUiDataTable().getRows());
dataModel = new PagedDataModel(pagedList, totalListSize);
return dataModel;
}
Página:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<ui:composition template="/template/template.xhtml">
<ui:define name="corpo">
<f:view>
<rich:panel bodyClass="paineis">
<f:facet name="header">
<h:outputText value="Consulta de Atendimentos"></h:outputText>
</f:facet>
<h:panelGrid>
<rich:dataTable value="#{atendimentoBean.dataModel}"
binding="#{atendimentoBean.uiDataTable}" var="_atendimentos"
rows="2" id="atendimentosTable" styleClass="tabelas"
rowKeyVar="rowIndex" rowClasses="even-row"
noDataLabel="Não existem atendimentos cadastradas.">
<f:facet name="header">
<rich:columnGroup>
<rich:column colspan="7">
<h:outputText value="Atendimentos" />
</rich:column>
<rich:column breakRowBefore="true">
<h:outputText value="Data da coleta" />
</rich:column>
<rich:column colspan="4">
<h:outputText value="Num. de ordem" />
</rich:column>
<rich:column>
<h:outputText value="Ações" />
</rich:column>
</rich:columnGroup>
</f:facet>
<rich:column>
<rich:collapsibleSubTableToggler for="sbtblrec" />
<h:outputText value="#{_atendimentos.dataColeta}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</rich:column>
<rich:column colspan="4">
<h:outputText value="#{_atendimentos.numOrdem}" />
</rich:column>
<rich:column style="text-align:center;">
<a4j:commandLink action="editarAtendimento">
<h:graphicImage library="images" name="alterar.png"
title="Editar" styleClass="imagemLink" width="20" height="20"
style="border:0" />
<f:setPropertyActionListener value="#{_atendimentos}"
target="#{atendimentoBean.atendimentoEdicao}">
</f:setPropertyActionListener>
</a4j:commandLink>
<h:commandLink id="linkExcluir" title="Excluir"
actionListener="#{atendimentoBean.editar}">
<rich:componentControl target="modalPanelExclusaoAtendimento"
operation="show">
</rich:componentControl>
<h:graphicImage library="images" name="excluir.png"
title="Excluir" styleClass="imagemLink" width="20" height="20"
style="border:0" />
</h:commandLink>
<rich:popupPanel id="modalPanelExclusaoAtendimento"
resizeable="true" modal="true" width="300" height="110">
<f:facet name="header">
<h:outputText value="Confirmação de exclusão" />
</f:facet>
<h:outputText value="Deseja realmente excluir o item?" />
<h:panelGrid columns="2"
style="margin-left:70px; margin-top:10px;">
<h:panelGroup>
<a4j:commandButton value="Cancelar"
onclick="#{rich:component('modalPanelExclusaoAtendimento')}.hide(); return false;">
</a4j:commandButton>
<a4j:commandButton value="Excluir" render="atendimentosTable"
execute="@this" action="#{atendimentoBean.excluir}"
immediate="true"
onclick="#{rich:component('modalPanelExclusaoAtendimento')}.hide()">
<f:setPropertyActionListener value="#{_atendimentos}"
target="#{atendimentoBean.atendimentoEdicao}">
</f:setPropertyActionListener>
</a4j:commandButton>
</h:panelGroup>
</h:panelGrid>
</rich:popupPanel>
</rich:column>
<rich:collapsibleSubTable value="#{_atendimentos.receptores}"
var="receptor" expandMode="client" id="sbtblrec">
<rich:column>
<f:facet name="header">
<h:outputText value="Nome do receptor" />
</f:facet>
<h:outputText value="#{receptor.nomeReceptor}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Num. bolsa receptor" />
</f:facet>
<h:outputText value="#{receptor.numBolsa}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Tipo Sanguineo" />
</f:facet>
<h:outputText value="#{receptor.tipoSanguineo}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="RH" />
</f:facet>
<h:outputText value="#{receptor.tipoRh}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Nome Hospital" />
</f:facet>
<h:outputText value="#{receptor.nomeHospital}" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Num Prontuário" />
</f:facet>
<h:outputText value="#{receptor.numProntuario}" />
</rich:column>
</rich:collapsibleSubTable>
<rich:collapsibleSubTable value="#{_atendimentos.doadores}"
var="doador" expandMode="client" id="sbtbldoad">
<rich:column colspan="6">
<f:facet name="header">
<h:outputText value="Numero de bolsas dos doadores" />
</f:facet>
<h:outputText value="#{doador.numBolsa}" />
</rich:column>
</rich:collapsibleSubTable>
<f:facet name="footer">
<rich:dataScroller for="atendimentosTable" maxPages="13"
stepControls="hide" />
</f:facet>
</rich:dataTable>
</h:panelGrid>
</rich:panel>
</f:view>
</ui:define>
</ui:composition>
</html>
Chamada da página através do menu
<rich:panelMenuItem label="Consultar Transfusões" execute="@all"
action="listaTransfusoes" mode="server" />