Utilizzo di DataTables

Forum relativo ai framework per l'utilizzo della tecnologia JSP

Utilizzo di DataTables

Messaggioda alesslud » mer lug 05, 2006 3:31 pm

In questo esempio si mostrerà l'utilizzo di un DataTable con le JSF.

L'esempio è tratto dal libro Core Java Server Faces di David Geary e Cay Horstmann http://www.horstmann.com/corejsf/
Ultima modifica di alesslud il mer lug 05, 2006 3:43 pm, modificato 1 volta in totale.
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » mer lug 05, 2006 3: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.corejsf.messages" var="msgs"/>
<title>
<h:outputText value="#{msgs.pageTitle}"/>
</title>
</head>
<body>
<h:form>
<h:dataTable value="#{customer.all}" var="customer"
styleClass="customers"
headerClass="customersHeader" columnClasses="custid,name">
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.customerIdHeader}"/>
</f:facet>
<h:outputText value="#{customer.Cust_ID}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.nameHeader}"/>
</f:facet>
<h:outputText value="#{customer.Name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.phoneHeader}"/>
</f:facet>
<h:outputText value="#{customer.Phone_Number}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.addressHeader}"/>
</f:facet>
<h:outputText value="#{customer.Street_Address}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.cityHeader}"/>
</f:facet>
<h:outputText value="#{customer.City}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.stateHeader}"/>
</f:facet>
<h:outputText value="#{customer.State}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{msgs.mioTesto}"/>
</f:facet>
<h:outputText value="#{customer.MioTesto}"/>
</h:column>
</h:dataTable>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Inseriamo i dati nel DB

Messaggioda alesslud » mer lug 05, 2006 3:34 pm

insert into customers (Cust_Id, Name, Phone_number,Street_address, City, State, MioTesto) VALUES (1,'Alessandro','3333','Mia via','Città','Mio stato','Mio testo')
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

faces-config.xml

Messaggioda alesslud » mer lug 05, 2006 3:34 pm

<?xml version="1.0"?>

<!--
Copyright 2003 Sun Microsystems, Inc. All rights reserved.
SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
-->

<!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>customer</managed-bean-name>
<managed-bean-class>com.corejsf.CustomerBean</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

web.xml

Messaggioda alesslud » mer lug 05, 2006 3:35 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>

<resource-ref>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

CustomerBean.java

Messaggioda alesslud » mer lug 05, 2006 3:35 pm

package com.corejsf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import com.aquilasiti.connessioni.ConnectionMySql;

public class CustomerBean {
private Connection conn;

/*public void open() throws SQLException, NamingException {
if (conn != null) return;
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
conn = ds.getConnection();
}*/
public void open() throws SQLException, NamingException {
if (conn != null) return;
conn = ConnectionMySql.getConnection();
}

public Result getAll() throws SQLException, NamingException {
try {
open();
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM Customers");
return ResultSupport.toResult(result);
} finally {
close();
}
}

public void close() throws SQLException {
if (conn == null) return;
conn.close();
conn = null;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages.properties

Messaggioda alesslud » mer lug 05, 2006 3:36 pm

pageTitle=Displaying Database Tables
customerIdHeader=Customer ID
nameHeader=Name
phoneHeader=Phone Number
addressHeader=Address
cityHeader=City
stateHeader=State
mioTesto=Mio Testo
refreshFromDB=Read from database
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

ConnectionMySql.java

Messaggioda alesslud » mer lug 05, 2006 3:36 pm

package com.aquilasiti.connessioni;

import java.sql.DriverManager;
import java.sql.Connection;

public class ConnectionMySql {

/**
* costruttore di default.
*/
public ConnectionMySql() {
super();
}

public static Connection getConnection() {
// pagina di autenticazione
// crea la connessione

try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.getMessage());

} catch (java.lang.NullPointerException cfe) {
System.out.println(cfe);

}

Connection conn = null;
try {
// conn = ServiceLocator.getConnection();

conn = DriverManager
.getConnection("jdbc:mysql://localhost/jsf","username","passwprd");
} catch (Exception sqle) {
System.out.println(sqle.getMessage());
sqle.printStackTrace();

}
return conn;

}
}
Ultima modifica di alesslud il ven feb 22, 2008 10:01 pm, modificato 1 volta in totale.
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

style.css

Messaggioda alesslud » mer lug 05, 2006 3:37 pm

.customers {
border: thin solid black;
}
.customersHeader {
text-align: center;
font-style: italic;
color: Snow;
background: Teal;
}
.customersFooter {
font-size: 0.75em;
text-align: center;
background: PowderBlue;
}
.delete {
text-align: center;
color: Black;
background: Red;
}
.custid {
height: 25px;
text-align: center;
background: MediumTurquoise;
}
.name {
text-align: left;
background: PowderBlue;
}
.firstName {
width: 6em;
text-align: left;
color: Black;
background: MediumTurquoise;
}
.age {
width: 3em;
text-align: center;
color: Black;
background: PowderBlue;
}
.sex {
width: 3em;
text-align: center;
color: Black;
background: MediumTurquoise;
}
.city {
width: 9em;
text-align: center;
color: Black;
background: PowderBlue;
}
.state {
width: 3em;
text-align: center;
color: Black;
background: MediumTurquoise;
}
.zip {
width: 6em;
text-align: center;
background: PowderBlue;
}
.evenRow {
background: PowderBlue;
}
.oddRow {
background: MediumTurquoise;
text-style: italic;
}
.pageLink {
font-size: 0.75em;
}
.editableControl {
text-align:Left;
}
.controlPanel {
text-align:left;
}
.page {
text-align:left;
}
.bulbImage {
border-width: 0px;
}
.emphasis {
font-size: 1.35em;
font-style: italic;
}
.value {
font-style: italic;
}
.controlLeft {
width: 45px;
vertical-align: top;
text-align:left;
}
.controlMiddle {
width: 50px;
vertical-align: top;
text-align:left;
}
.controlRight {
width: 40px;
vertical-align: top;
text-align:left;
}
.pageLinks {
border: thin solid Black;
background: PowderBlue;
}
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