Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
ppt
Corso JSF.ppt 783.9 kB 1 02-Sep-2010 07:52 m.sanfilippo
zip
SmeaCrud-src.zip 31.7 kB 1 31-Aug-2010 16:02 Administrator
war
SmeaCrud.war 30.9 kB 1 31-Aug-2010 16:02 Administrator
gif
jsf.gif 5.7 kB 1 23-Aug-2010 19:42 Administrator

This page (revision-70) was last changed on 02-Sep-2010 09:10 by m.sanfilippo  

This page was created on 23-Aug-2010 19:35 by Administrator

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Difference between version and

At line 1 changed one line
!!Sorgenti del corso
!!Risorse del Corso JSF 2.0 Base
At line 3 added 5 lines
%%tabbedSection
%%tab-Requisiti
!!Requisiti
;IDE:__Netbeans 6.9__ versione Java Web oppure __Eclipse 3.5.2__ con Server Adapter "Glassfish v3 JavaEE6"
;Application Server: Glassfish v3 (Java EE6)
At line 4 changed 2 lines
!!Parte 1
!index.html (v1)
/%
%%tab-Step1
!!Step 1: La navigazione
!index.html
At line 25 changed one line
!list.xhtml (v1)
!list.xhtml
At line 46 changed 2 lines
!!Parte 2
!masterLayout.xhtml (v1)
/%
%%tab-Step2
!!Step 2: Facelets templating
!Logo per l'header
[{Image src='./cartella/sottocartella/header_logo.gif'}]
[salva con nome... |../cartella/sottocartella/header_logo.gif]
!masterLayout.xhtml
At line 62 changed one line
<h:graphicImage library="images" name="jsf.gif"/>
<h:graphicImage library="images" name="header_logo.gif"/>
At line 92 added 6 lines
!index.xhtml
%%prettify
{{{
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/template/masterLayout.xhtml">
At line 99 added 836 lines
<ui:define name="content">
<h:commandButton action="views/list" value="Entra"/>
</ui:define>
</ui:composition>
}}}
/%
!list.xhtml
%%prettify
{{{
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/template/masterLayout.xhtml">
<ui:define name="content">
<h1>
<h:outputText value="Hello World!!!"/>
</h1>
</ui:define>
</ui:composition>
}}}
/%
!style.css
%%prettify
{{{
body {
font-family: Verdana,Arial,Helvetica,sans-serif;
font-size: 11px;
margin: 0px;
padding: 0px;
}
#header {
width: 100%;
margin-bottom: 10px;
border-bottom: 2px solid #999;
color:#000066;
font-size: 30px;
}
}}}
/%
/%
%%tab-Step3
!!Step 3: i Managed Baen e lo scope
!ApplicationController.java
%%prettify
{{{
package corsojsf.managedbeans;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
@ManagedBean
@RequestScoped
public class ApplicationController {
private int random=-1;
public String paginaLista(){
return "views/list";
}
public int getNumeroRandom(){
if (random==-1)
random= (int)(Math.random()*100);
return random;
}
}
}}}
/%
!marsterLayout.xhtml (porzione)
%%prettify
{{{
[...]
JSF Sample Application #{applicationController.numeroRandom}
[...]
}}}
/%
!index.xhtml (porzione)
%%prettify
{{{
[...]
<ui:define name="content">
<h:commandButton action="#{applicationController.paginaLista}" value="Entra"/>
</ui:define>
[...]
}}}
/%
/%
%%tab-Step4
!!Step 4: inclusione con facelets e i componenti tabella
!Camera.java
%%prettify
{{{
package corsojsf.model;
import java.math.BigDecimal;
public class Camera {
private String nome;
private Integer numeroPosti;
private BigDecimal prezzo;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Integer getNumeroPosti() {
return numeroPosti;
}
public void setNumeroPosti(Integer numeroPosti) {
this.numeroPosti = numeroPosti;
}
public BigDecimal getPrezzo() {
return prezzo;
}
public void setPrezzo(BigDecimal prezzo) {
this.prezzo = prezzo;
}
}
}}}
/%
!Prenotazione.java
%%prettify
{{{
package corsojsf.model;
import java.util.Date;
public class Prenotazione {
private String cliente;
private Date dataInizio;
private Date dataFine;
private Camera camera;
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public String getCliente() {
return cliente;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public Date getDataFine() {
return dataFine;
}
public void setDataFine(Date dataFine) {
this.dataFine = dataFine;
}
public Date getDataInizio() {
return dataInizio;
}
public void setDataInizio(Date dataInizio) {
this.dataInizio = dataInizio;
}
}
}}}
/%
!ListController.java
%%prettify
{{{
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corsojsf.managedbeans;
import corsojsf.model.Camera;
import corsojsf.model.Prenotazione;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class ListController {
private List<Camera> camere = new LinkedList<Camera>();
private List<Prenotazione> prenotazioni = new LinkedList<Prenotazione>();
private Camera camera = new Camera();
private Prenotazione prenotazione = new Prenotazione();
public List<Camera> getCamere() {
return camere;
}
public List<Prenotazione> getPrenotazioni() {
return prenotazioni;
}
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public Prenotazione getPrenotazione() {
return prenotazione;
}
public void setPrenotazione(Prenotazione prenotazione) {
this.prenotazione = prenotazione;
}
}
}}}
/%
!list.xhtml
%%prettify
{{{
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/template/masterLayout.xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="content">
<ui:include src="/sections/list/intestazioneCamere.xhtml">
<ui:param name="titolo" value="Lista Camere"/>
</ui:include>
<ui:include src="/sections/list/tabellaCamere.xhtml"/>
</ui:define>
</ui:composition>
}}}
/%
!tabellaCamere.xhtml
%%prettify
{{{
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:dataTable styleClass="lista" value="#{listController.camere}" var="camera">
<h:column>
<f:facet name="header">
<h:outputText value="nome"/>
</f:facet>
<h:outputText value="#{camera.nome}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="n.posti"/>
</f:facet>
<h:outputText value="#{camera.numeroPosti}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="prezzo"/>
</f:facet>
<h:outputText value="#{camera.prezzo}"/>
</h:column>
<h:column>
<h:outputLink value="Edit"/>
#160;
<h:outputLink value="Del"/>
</h:column>
</h:dataTable>
</ui:composition>
}}}
/%
!intestazioneCamere.xhtml
%%prettify
{{{
<ui:composition 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">
<h3>
<h:outputText value="Lista Camere"/>
</h3>
<h:commandLink action="#{listController.nuovaCamera}" value="Nuova"/>
</ui:composition>
}}}
/%
!intestazioneCamere.xhtml (porzione)
%%prettify
{{{
[...]
<h3>
<h:outputText value="#{titolo}"/>
</h3>
<h3>
<h:outputText style="display:block" value="Nessuna camera trovata"
rendered="#{empty listController.camere}"/>
</h3>
[...]
}}}
/%
!tabellaCamere.xhtml (porzione)
%%prettify
{{{
[...]
<h:dataTable styleClass="lista" value="#{listController.camere}"
var="camera" rendered="#{!empty listController.camere}">
[...]
}}}
/%
/%
%%tab-Step5
!!Step 5: form di inserimento, componenti I/O e metodi action.
!camera.xhtml
%%prettify
{{{
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/template/masterLayout.xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="content">
<h:panelGrid columns="2">
<h:outputLabel for="nome" value="nome camera"/>
<h:inputText id="nome" value="#{listController.camera.nome}"/>
<h:outputLabel for="posti" value="posti letto"/>
<h:inputText id="posti" value="#{listController.camera.numeroPosti}"/>
<h:outputLabel for="prezzo" value="prezzo"/>
<h:inputText id="prezzo" value="#{listController.camera.prezzo}"/>
<h:commandButton value="annulla" action="/views/list" immediate="true"/>
<h:commandButton value="salva" action="#{listController.salvaCamera}"/>
</h:panelGrid>
</ui:define>
</ui:composition>
}}}
/%
!ListController.java (porzione)
%%prettify
{{{
/*Azioni*/
public String nuovaCamera() {
setCamera(new Camera());
return "/views/camera";
}
public String salvaCamera() {
Camera daSalvare = getCamera();
if (getCamere() == null) {
this.camere = new LinkedList<Camera>();
}
if (estraiCamera(daSalvare.getNome()) != null) {
getCamere().remove(estraiCamera(daSalvare.getNome()));
}
getCamere().add(daSalvare);
setCamera(null);
return "/views/list";
}
private Camera estraiCamera(String nome) {
for (Camera c : camere) {
if (c.getNome().equalsIgnoreCase(nome)) {
return c;
}
}
return null;
}
}}}
/%
/%
%%tab-Step6
!!Step 6: metodi action con parametro
!list.xhtml (porzione)
%%prettify
{{{
<h:column>
<h:commandLink value="Edit" action="#{listController.modificaCamera(camera)}"/>
-
<h:commandLink value="Del" action="#{listController.eliminaCamera(camera)}"/>
</h:column>
}}}
/%
!ListController.java (porzione)
%%prettify
{{{
public String modificaCamera(Camera camera) {
setCamera(camera);
return "/views/camera";
}
public void eliminaCamera(Camera camera)
{
getCamere().remove(camera);
}
}}}
/%
/%
%%tab-Step7
!!Step 7: i convertitori e i componenti "select"
!prenotazione.xhtml
%%prettify
{{{
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="/template/masterLayout.xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="content">
<h:panelGrid columns="2">
<h:outputLabel for="camera" value="camera"/>
<h:selectOneMenu id="camera" value="#{listController.prenotazione.camera}">
<f:selectItems value="#{listController.camere}" var="camera" itemValue="#{camera}" itemLabel="#{camera.nome}"/>
</h:selectOneMenu>
<h:outputLabel for="cliente" value="cliente"/>
<h:inputText id="cliente" value="#{listController.prenotazione.cliente}" required="true">
</h:inputText>
<h:outputLabel for="dal" value="Dal"/>
<h:inputText id="dal" value="#{listController.prenotazione.dataInizio}" required="true">
<f:convertDateTime pattern="yyyymmdd" />
</h:inputText>
<h:outputLabel for="al" value="Al"/>
<h:inputText id="al" value="#{listController.prenotazione.dataFine}" required="true">
<f:convertDateTime pattern="yyyymmdd" />
</h:inputText>
<h:commandButton value="annulla" action="/views/list" immediate="true"/>
<h:commandButton value="salva" action="#{listController.salvaPrenotazione}"/>
</h:panelGrid>
</ui:define>
</ui:composition>
}}}
/%
!CameraConverter.java
%%prettify
{{{
package corsojsf.managedbeans;
import corsojsf.model.Camera;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
@FacesConverter(forClass=Camera.class)
public class CameraConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
List<Camera> items = (List<Camera>) ((UISelectItems) component.getChildren().get(0)).getValue();
for (Camera camera : items) {
if (camera.getNome().equals(value)) {
return camera;
}
}
throw new ConverterException("Impossibile convertire " + value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value instanceof Camera) {
return ((Camera) value).getNome();
} else {
throw new IllegalArgumentException("Impossibile convertire oggetti " + value.getClass().getCanonicalName());
}
}
}
}}}
/%
!ListController.java (porzione)
%%prettify
{{{
[...]
public String nuovaPrenotazione() {
setPrenotazione(new Prenotazione());
return "/views/prenotazione";
}
public String modificaPrenotazione(Prenotazione prenotazione) {
setPrenotazione(prenotazione);
return "/views/prenotazione";
}
public void eliminaPrenotazione(Prenotazione prenotazione)
{
getPrenotazioni().remove(prenotazione);
}
public String salvaPrenotazione() {
Prenotazione daSavlare = getPrenotazione();
if (getPrenotazioni() == null) {
this.prenotazioni = new LinkedList<Prenotazione>();
}
if (this.prenotazioni.contains(daSavlare)) {
getPrenotazioni().remove(daSavlare);
}
getPrenotazioni().add(daSavlare);
setPrenotazione(new Prenotazione());
return "/views/list";
}
[...]
}}}
/%
!tabellaPrenotazione.xhtml (porzione)
%%prettify
{{{
[...]
<h:column>
<f:facet name="header">
<h:outputText value="Dal"/>
</f:facet>
<h:outputText value="#{prenotazione.dataInizio}">
<f:convertDateTime pattern="dd/mm/yyyy"/>
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="al"/>
</f:facet>
<h:outputText value="#{prenotazione.dataFine}">
<f:convertDateTime pattern="dd/mm/yyyy"/>
</h:outputText>
</h:column>
[...]
}}}
/%
!Camera.java (porzione)
%%prettify
{{{
[...]
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Camera other = (Camera) obj;
if ((this.nome == null) ? (other.nome != null) : !this.nome.equals(other.nome)) {
return false;
}
return true;
}
[...]
}}}
/%
!Prenotazione.java (porzione)
%%prettify
{{{
[...]
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Prenotazione other = (Prenotazione) obj;
if ((this.cliente == null) ? (other.cliente != null) : !this.cliente.equals(other.cliente)) {
return false;
}
if (this.dataInizio != other.dataInizio && (this.dataInizio == null || !this.dataInizio.equals(other.dataInizio))) {
return false;
}
if (this.dataFine != other.dataFine && (this.dataFine == null || !this.dataFine.equals(other.dataFine))) {
return false;
}
if (this.camera != other.camera && (this.camera == null || !this.camera.equals(other.camera))) {
return false;
}
return true;
}
[...]
}}}
/%
/%
%%tab-Step8
!!Step 8: validazione standard e metodi di validazione
!camera.xhtml (porzione)
%%prettify
{{{
[...]
<h:outputLabel for="posti" value="posti letto"/>
<h:inputText id="posti" value="#{listController.camera.numeroPosti}"
validatorMessage="Il numero di posti deve essere compreso tra 1 e 5">
<f:validateLongRange minimum="1" maximum="5"/>
</h:inputText>
<h:outputLabel for="prezzo" value="prezzo"/>
<h:inputText id="prezzo" value="#{listController.camera.prezzo}"
validator="#{listController.validatePrezzo}"/>
[...]
}}}
/%
!ListController.java (porzione)
%%prettify
{{{
[...]
/*validator*/
public void validatePrezzo(FacesContext context, UIComponent component, Object enteredValue) throws ValidatorException {
BigDecimal prezzo=(BigDecimal)enteredValue;
if (prezzo.intValue()%2!=0){
throw new ValidatorException(
new FacesMessage("Errore di validazione: la parte intera del prezzo deve essere pari"));
}
}
[...]
}}}
/%
/%
%%tab-Step9
!!Step 9: messaggi, validazione "domain-level" e classi Validator
!masterLayout.xhtml (porzione)
%%prettify
{{{
[...]
<div id="header">
<h:graphicImage library="images" name="header_logo.gif"/>
JSF Sample Application #{applicationController.numeroRandom}
<div style="color:#770000;">
<h:messages showDetail="false" globalOnly="true"/>
</div>
</div>
[...]
}}}
/%
!camera.xhtml (porzione)
%%prettify
{{{
[...]
<h:message for="posti" style="color:red" />
<h:panelGrid columns="2">
<h:outputLabel for="nome" value="nome camera"/>
<h:inputText id="nome" value="#{listController.camera.nome}"/>
<h:outputLabel for="posti" value="posti letto"/>
<h:inputText id="posti" value="#{listController.camera.numeroPosti}"
validatorMessage="Il numero di posti deve essere compreso tra 1 e 5">
<f:validateLongRange minimum="1" maximum="5"/>
</h:inputText>
}}}
/%
!ListController.java (porzione)
%%prettify
{{{
[...]
public String salvaPrenotazione() {
Prenotazione daSalvare = getPrenotazione();
[...]
if (!verificaDisponibilita(daSalvare)) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Errore, la stanza non è disponibile nel periodo indicato",""));
return null;
}
[...]
}
[...]
private boolean verificaDisponibilita(Prenotazione daSalvare) {
boolean intersezione = false;
for (Prenotazione pre : prenotazioni) {
if (pre.getCamera().equals(daSalvare.getCamera())) {
if (pre.getDataFine().before(daSalvare.getDataInizio())) {
continue;
} else if (pre.getDataInizio().after(daSalvare.getDataFine())) {
continue;
} else {
intersezione = true;
}
}
}
return !intersezione;
}
[...]
}}}
/%
!CameraValidator.java
%%prettify
{{{
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corsojsf.managedbeans;
import corsojsf.model.Camera;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator(value="cameraValidator")
public class CameraValidator implements Validator{
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (!(value instanceof Camera))
throw new ValidatorException(
new FacesMessage("L'oggetto da passare a CameraValidator deve essere una camera"));
Camera camera= (Camera)value;
if (camera.getNumeroPosti()>2 && camera.getPrezzo().intValue() < 50)
throw new ValidatorException(
new FacesMessage("Le camere con più di 2 posti devono avere un prezzo > di 50"));
}
}
}}}
/%
!prenotazione.xtml (porzione)
%%prettify
{{{
[...]
<h:selectOneMenu id="camera" value="#{listController.prenotazione.camera}">
<f:selectItems value="#{listController.camere}" var="camera" itemValue="#{camera}" itemLabel="#{camera.nome}"/>
<f:validator validatorId="cameraValidator"/>
</h:selectOneMenu>
[...]
}}}
/%
/%
%%tab-Downloads
!!Downloads
||Descrizione||File
| Sorgenti dell'esempio completo | [SmeaCrud-src.zip]
| War dell'applicazione web | [SmeaCrud.war]
| Slide del corso | [Corso JSF.ppt]
/%
/%
[appoggio]
Version Date Modified Size Author Changes ... Change note
70 02-Sep-2010 09:10 23.707 kB m.sanfilippo to previous
69 02-Sep-2010 07:52 23.693 kB m.sanfilippo to previous | to last
68 02-Sep-2010 07:51 23.676 kB m.sanfilippo to previous | to last
67 01-Sep-2010 16:54 24.929 kB Administrator to previous | to last
66 01-Sep-2010 16:54 23.674 kB Administrator to previous | to last
65 01-Sep-2010 16:52 24.749 kB Administrator to previous | to last
64 01-Sep-2010 08:21 23.674 kB m.sanfilippo to previous | to last
63 01-Sep-2010 08:13 23.384 kB m.sanfilippo to previous | to last
62 31-Aug-2010 17:48 23.384 kB m.sanfilippo to previous | to last
61 31-Aug-2010 16:27 23.372 kB Administrator to previous | to last
« This page (revision-70) was last changed on 02-Sep-2010 09:10 by m.sanfilippo