h:dataTable: gestione, style e sorting. Un esempio

Forum relativo ai framework per l'utilizzo della tecnologia JSP

h:dataTable: gestione, style e sorting. Un esempio

Messaggioda alesslud » mar ott 06, 2009 2:23 pm

Vi mostriamo qui di seguito un esempio abbastanza completo per l'utilizzo di un dataTable con JSF.

L'esempio, oltre ad eseguire il sorting delle colonne, è utile anche per capire l'utilizzo dei fogli di style con h:dataTable.
Allegati
facestablesorting.zip
Testato con Eclipse Galileo
(15.8 KiB) Scaricato 348 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » mar ott 06, 2009 2:26 pm

<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<f:loadBundle basename="com.aquilasiti.messages" var="res" />
<title><h:outputText value="#{res.title}" /></title>
</head>
<body>
<h:form>
<h:dataTable value="#{tableData.names}" var="name" styleClass="names"
headerClass="namesHeader" footerClass="namesHeader"
columnClasses="name,cognome,note">
<h:column>
<f:facet name="header">
<h:commandLink action="#{tableData.names.sortByNome}">
<h:outputText value="#{res.nomeColumnHeader}" />
</h:commandLink>
</f:facet>
<h:outputText value="#{name.nome}" />
<f:facet name="footer">
<h:commandLink action="#{tableData.names.reSortByNome}">
<h:outputText value="#{res.nomeColumnHeader}" />
</h:commandLink>
</f:facet>
<f:verbatim>,</f:verbatim>
</h:column>
<h:column>
<f:facet name="header">
<h:commandLink action="#{tableData.names.sortByCognome}">
<h:outputText value="#{res.cognomeColumnHeader}" />
</h:commandLink>
</f:facet>
<h:outputText value="#{name.cognome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:commandLink action="#{tableData.names.sortByNote}">
<h:outputText value="#{res.noteHeader}" />
</h:commandLink>
</f:facet>
<h:outputText value="#{name.note}" />
</h:column>
</h:dataTable>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

styles.css

Messaggioda alesslud » mar ott 06, 2009 2:26 pm

.customersHeader {
text-align: center;
font-style: italic;
color: Snow;
background: Teal;
}

.names {
border: thin solid black;
}
.namesHeader {
text-align: center;
font-style: italic;
color: silver;
background: silver;
}

.cognome {
height: 25px;
text-align: center;
background: MediumTurquoise;
}

.name {
text-align: left;
background: PowderBlue;
}

.note {
width: 9em;
text-align: center;
color: Black;
background: PowderBlue;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » mar ott 06, 2009 2:26 pm

<?xml version="1.0"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

faces-config.xml

Messaggioda alesslud » mar ott 06, 2009 2:27 pm

<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>tableData</managed-bean-name>
<managed-bean-class>com.aquilasiti.TableData</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

MyBean.java

Messaggioda alesslud » mar ott 06, 2009 2:28 pm

package com.aquilasiti;

public class MyBean {
private String nome;
private String cognome;
private String note;

public MyBean(String first, String last, String note) {
this.nome = first;
this.cognome = last;
this.note = note;
}

public String getNote() {
return note;
}

public void setNote(String note) {
this.note = note;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}

}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

SortModel.java

Messaggioda alesslud » mar ott 06, 2009 2:29 pm

package com.aquilasiti;

import java.util.Arrays;
import java.util.Comparator;
import javax.faces.model.DataModel;
import javax.faces.model.DataModelListener;

public class SortModel extends DataModel {
private DataModel model;
private RowTable[] rows;

private static Comparator byCognome = new Comparator() {
public int compare(Object o1, Object o2) {
RowTable r1 = (RowTable) o1;
RowTable r2 = (RowTable) o2;
MyBean n1 = (MyBean) r1.getData();
MyBean n2 = (MyBean) r2.getData();
return n1.getCognome().compareTo(n2.getCognome());
}
};

private static Comparator byNome = new Comparator() {
public int compare(Object o1, Object o2) {
RowTable r1 = (RowTable) o1;
RowTable r2 = (RowTable) o2;
MyBean n1 = (MyBean) r1.getData();
MyBean n2 = (MyBean) r2.getData();
return n1.getNome().compareTo(n2.getNome());
}
};

private static Comparator byNote = new Comparator() {
public int compare(Object o1, Object o2) {
RowTable r1 = (RowTable) o1;
RowTable r2 = (RowTable) o2;
MyBean n1 = (MyBean) r1.getData();
MyBean n2 = (MyBean) r2.getData();
return n1.getNote().compareTo(n2.getNote());
}
};



private static Comparator reByNome = new Comparator() {
public int compare(Object o1, Object o2) {
RowTable r1 = (RowTable) o1;
RowTable r2 = (RowTable) o2;
MyBean n1 = (MyBean) r1.getData();
MyBean n2 = (MyBean) r2.getData();
return n2.getNome().compareTo(n1.getNome());
}
};


private class RowTable {
private int row;

public RowTable(int row) {
this.row = row;
}

public Object getData() {
int originalIndex = model.getRowIndex();
model.setRowIndex(row);
Object thisRowData = model.getRowData();
model.setRowIndex(originalIndex);
return thisRowData;
}
}

public SortModel(DataModel model) {
this.model = model;
int rowCnt = model.getRowCount();
if (rowCnt != -1) {
rows = new RowTable[rowCnt];
for (int i = 0; i < rowCnt; ++i) {
rows[i] = new RowTable(i);
}
}
}

public String sortByNome() {
Arrays.sort(rows, byNome);
return null;
}

public String sortByCognome() {
Arrays.sort(rows, byCognome);
return null;
}

public String sortByNote() {
Arrays.sort(rows, byNote);
return null;
}


public String reSortByNome() {
Arrays.sort(rows, reByNome);
return null;
}


public void setRowIndex(int rowIndex) {
if (rowIndex == -1 || rowIndex >= model.getRowCount()) {
model.setRowIndex(rowIndex);
} else {
model.setRowIndex(rows[rowIndex].row);
}
}

// The following methods delegate directly to the
// decorated model

public boolean isRowAvailable() {
return model.isRowAvailable();
}

public int getRowCount() {
return model.getRowCount();
}

public Object getRowData() {
return model.getRowData();
}

public int getRowIndex() {
return model.getRowIndex();
}

public Object getWrappedData() {
return model.getWrappedData();
}

public void setWrappedData(Object obj) {
model.setWrappedData(obj);
}

public void addDataModelListener(DataModelListener listener) {
model.addDataModelListener(listener);
}

public DataModelListener[] getDataModelListeners() {
return model.getDataModelListeners();
}

public void removeDataModelListener(DataModelListener listener) {
model.removeDataModelListener(listener);
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

TableData.java

Messaggioda alesslud » mar ott 06, 2009 2:29 pm

package com.aquilasiti;

import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

public class TableData {

private DataModel filterModel = null;
private static final MyBean[] names = new MyBean[] {
new MyBean("Alessandro", "Ludovici","Ecceziunal"), new MyBean("Pippo", "Carluccio","Fregnuccio"),
new MyBean("Giorgino", "Giorgetto","Burinaccio"), new MyBean("Picchio", "Forte","e meno"),
new MyBean("Vendo", "Patate","Arrosto") };



public TableData() {
ArrayDataModel model = new ArrayDataModel(names);
filterModel = new SortModel(model);
}

public DataModel getNames() {
return filterModel;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages.properties

Messaggioda alesslud » mar ott 06, 2009 2:30 pm

title=Eseguire un sorting in una h:dataTable
pageTitle=Una lista di nomi:
cognomeColumnHeader=Cognome
nomeColumnHeader=Nome
noteHeader=Note
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am


Torna a JSF e Struts

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron