Editing di un dataTable con JSF: h:dataTable

Forum relativo ai framework per l'utilizzo della tecnologia JSP

Come reputi il seguente articolo?

Ottimo
0
Nessun voto
Buono
1
100%
Sufficiente
0
Nessun voto
Mediocre
0
Nessun voto
Scarso
0
Nessun voto
 
Voti totali : 1

Editing di un dataTable con JSF: h:dataTable

Messaggioda alesslud » gio ott 08, 2009 1:32 pm

L'applicazione di questo esempio cercherà di dare una panoramica relativa all'utilizzo di un dataTable con JSF.
Allegati
facestableediting.zip
Testato con Eclipse Galileo
(17.41 KiB) Scaricato 296 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsf

Messaggioda alesslud » gio ott 08, 2009 1:33 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.pageTitle}"/>
</title>
</head>
<body>
<h:form>
<h:dataTable value="#{myData.names}" var="name"
styleClass="names" headerClass="namesHeader"
columnClasses="a,b, c, d">
<!-- abilita le checkbox -->
<h:column rendered="#{myData.editable}">
<f:facet name="header">
<h:outputText value="#{res.deleteColumnHeader}"/>
</f:facet>
<h:selectBooleanCheckbox value="#{name.markedForDeletion}"
onclick="submit()"/>
</h:column>
<h:column rendered="#{myData.editable}">
<f:facet name="header">
<h:outputText value="#{res.editColumnHeader}"/>
</f:facet>
<h:selectBooleanCheckbox value="#{name.editable}"
onclick="submit()"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{res.cognomeColumnHeader}"/>
</f:facet>
<h:inputText value="#{name.cognome}" rendered="#{name.editable}"
size="10"/>
<h:outputText value="#{name.cognome}"
rendered="#{not name.editable}"/>
<f:verbatim>,</f:verbatim>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{res.nomeColumnHeader}"/>
</f:facet>
<h:inputText value="#{name.nome}"
rendered="#{name.editable}" size="10"/>
<h:outputText value="#{name.nome}"
rendered="#{not name.editable}"/>
</h:column>
</h:dataTable>
<h:commandButton value="#{res.deleteButtonText}"
rendered="#{myData.editable}"
action="#{myData.deleteNames}"
disabled="#{not myData.anyNamesMarkedForDeletion}"/>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

styles.css

Messaggioda alesslud » gio ott 08, 2009 1:33 pm

.names {
border: thin solid black;
}
.namesHeader {
text-align: center;
font-style: italic;
color: Snow;
background: Teal;
}
.a {
height: 25px;
text-align: center;
background: MediumTurquoise;
}
.b {
text-align: left;
background: PowderBlue;
}

.c {
height: 25px;
text-align: center;
background: aqua;
}
.d {
text-align: left;
background: gray;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » gio ott 08, 2009 1:36 pm

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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 » gio ott 08, 2009 1:36 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>myData</managed-bean-name>
<managed-bean-class>com.aquilasiti.MyData</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

messages.properties

Messaggioda alesslud » gio ott 08, 2009 1:36 pm

pageTitle=Editing Data Table
editColumnHeader=Edit
nomeColumnHeader=Nome
cognomeColumnHeader=Cognome
deleteColumnHeader=Delete
deleteButtonText=Elimina i valori selezionati
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

MyData.java

Messaggioda alesslud » gio ott 08, 2009 1:37 pm

package com.aquilasiti;

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

public class MyData {
//abilitano le checkbox
private boolean editable = true;
private ArrayDataModel model = null;

private static final MyBean[] names = {
new MyBean("Alessandro", "Ludovici"),
new MyBean("Carlo", "Orsetto"),
new MyBean("Guido", "Maluccio"),
new MyBean("Guido", "Piano"),
new MyBean("Vado", "Lontano"),
};

public MyData() { model = new ArrayDataModel(names); }

public DataModel getNames() { return model; }

public boolean isEditable() { return editable; }
public void setEditable(boolean newValue) { editable = newValue; }

public String deleteNames() {
if (!getAnyNamesMarkedForDeletion())
return null;

MyBean[] currentNames = (MyBean[]) model.getWrappedData();
MyBean[] newNames = new MyBean[currentNames.length
- getNumberOfNamesMarkedForDeletion()];

for(int i = 0, j = 0; i < currentNames.length; ++i) {
MyBean name = (MyBean) currentNames[i];
if (!name.isMarkedForDeletion()) {
newNames[j++] = name;
}
}
model.setWrappedData(newNames);
return null;
}

public int getNumberOfNamesMarkedForDeletion() {
MyBean[] currentNames = (MyBean[]) model.getWrappedData();
int cnt = 0;

for(int i = 0; i < currentNames.length; ++i) {
MyBean name = (MyBean) currentNames[i];
if (name.isMarkedForDeletion())
++cnt;
}
return cnt;
}

public boolean getAnyNamesMarkedForDeletion() {
MyBean[] currentNames = (MyBean[]) model.getWrappedData();
for(int i = 0; i < currentNames.length; ++i) {
MyBean name = (MyBean) currentNames[i];
if (name.isMarkedForDeletion())
return true;
}
return false;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

MyBean.java

Messaggioda alesslud » gio ott 08, 2009 1:37 pm

package com.aquilasiti;

public class MyBean {
private String nome;
private String cognome;
private boolean markedForDeletion = false;
private boolean editable;

public boolean isEditable() {
return editable;
}

public void setEditable(boolean editable) {
this.editable = editable;
}

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

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;
}

public boolean isMarkedForDeletion() {
return markedForDeletion;
}

public void setMarkedForDeletion(boolean newValue) {
markedForDeletion = newValue;
}
}
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