Reputation: 385
I'm doing a hotel room reservation page with Java Server Faces. I have a base template that contains a hotel search, with an index template that is where I show the results. I'm loading all the hotels on the front page, like cover. And I show only 9 hotels so that they do not overflow the hotels of the DIV:
Ok, I have only 10 hotels registered in the database in total .... I want to make a pagination for my page where I display only 9 hotels per page .... the result of having this, as I have 10 hotels in the database, it would be that on the next page I should only see 1 single hotel.
This is my index 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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:viewAction action="#{buscador.cargarPortada()}"/>
</f:metadata>
<body>
<ui:composition template="./templates/base.xhtml">
<ui:define name="content">
<h:form id="form2">
<h:dataTable value="#{buscador.display}" var="res">
<h:column>
<h:outputLabel value="#{res.a}" escape="false"/>
</h:column>
<h:column>
<h:outputLabel value="#{res.b}" escape="false"/>
</h:column>
<h:column>
<h:outputLabel value="#{res.c}" escape="false"/>
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
</body>
I am using a dataTable to display the results. I have two classes: a class that handles rows of the dataTable, and another class that constructs the display boxes of each hotel
With this class I construct each box in the table:
package Clases;
public class ResultElement {
private String nombre, imagen, localidad;
private String display;
public ResultElement(String nombre, String imagen, String localidad){
this.nombre=nombre;
this.imagen=imagen;
this.localidad=localidad;
display = "<div id='HotP'><b><p style=\"text-align:center;font-
style:italic;color:red\">" + nombre + "</p></b>"
+"<img src=\"resources/images/" + imagen +
"\"style=\"width:160px;height:70px;border:1px solid gray;\"/><br>"
+ "<span style=\"color: darkblue;text-align: center;\">"+
localidad +"</span></div>";
}
/**
* @return the nombre
*/
public String getNombre() {
return nombre;
}
/**
* @param nombre the nombre to set
*/
public void setNombre(String nombre) {
this.nombre = nombre;
}
/**
* @return the imagen
*/
public String getImagen() {
return imagen;
}
/**
* @param imagen the imagen to set
*/
public void setImagen(String imagen) {
this.imagen = imagen;
}
/**
* @return the localidad
*/
public String getLocalidad() {
return localidad;
}
/**
* @param localidad the localidad to set
*/
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
/**
* @return the display
*/
public String getDisplay() {
return display;
}
/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}
}
With this class I manage the rows of the table. I show 3 results per row:
package Clases;
public class DisplayResult {
private String A, B, C;
public DisplayResult(String a, String b, String c){
A = a;
B = b;
C = c;
}
/**
* @return the A
*/
public String getA() {
return A;
}
/**
* @param A the A to set
*/
public void setA(String A) {
this.A = A;
}
/**
* @return the B
*/
public String getB() {
return B;
}
/**
* @param B the B to set
*/
public void setB(String B) {
this.B = B;
}
/**
* @return the C
*/
public String getC() {
return C;
}
/**
* @param C the C to set
*/
public void setC(String C) {
this.C = C;
}
}
This class is where I will load the cover of the page and perform searches:
package Beans;
import Clases.DisplayResult;
import Clases.ResultElement;
import javax.inject.Named;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.sql.ResultSet;
import Database.GestorDB;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author SEMINARIO
*/
@Named(value = "portada")
@ManagedBean
@ViewScoped
public class Buscador {
private String fechaE, fechaS, localidad;
private int personas;
private static ArrayList<DisplayResult> displayList;
private ArrayList<String> aux = new ArrayList<>();
public Buscador() {
displayList = new ArrayList<>();
}
public String getFechaE() {
return fechaE;
}
public void setFechaE(String fechaE) {
this.fechaE = fechaE;
}
public String getFechaS() {
return fechaS;
}
public void setFechaS(String fechaS) {
this.fechaS = fechaS;
}
public String getLocalidad() {
return localidad;
}
public void setLocalidad(String localidad) {
this.localidad = localidad;
}
public int getPersonas() {
return personas;
}
public void setPersonas(int personas) {
this.personas = personas;
}
public ArrayList<DisplayResult> getDisplay() {
return displayList;
}
public void setDisplay(ArrayList<DisplayResult> listaResultado) {
Buscador.displayList = listaResultado;
}
public void agregarResultado(DisplayResult resultado){
Buscador.displayList.add(resultado);
}
public void limpiarResultados(){
Buscador.displayList.clear();
aux.clear();
}
public void calibrar(){
do{
if(aux.size()%3 != 0)
aux.add("");
}while(aux.size()%3 != 0);
}
public void cargarPortada(){
try{
ResultSet rs = GestorDB.getConsulta("SELECT HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
+ " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES FETCH FIRST 9 ROWS ONLY");
while(rs.next()){
ResultElement res = new ResultElement(rs.getString("A"), rs.getString("IMGHOTEL"),rs.getString("B"));
aux.add(res.getDisplay());
}
calibrar();
for(int i = 0; i < aux.size(); i+=3){
DisplayResult obj = new DisplayResult(aux.get(i),aux.get(i+1),aux.get(i+2));
displayList.add(obj);
}
} catch (SQLException ex) {
Logger.getLogger(Buscador.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I thought of controlling the pagination of my page with my arrayList but the truth is that I do not know how to place it in the dataTable ...
Is there an easy and practical way to do it?
Upvotes: 1
Views: 233
Reputation: 8151
Native JSF does not have any support/have components for pagination.
You could go for a Component library like Primefaces, which has a pagination components like DataGrid .
Upvotes: 2