Conversione e Validazione con JSF

Forum relativo ai framework per l'utilizzo della tecnologia JSP

Come reputi l'esempio?

Ottimo
0
Nessun voto
Buono
0
Nessun voto
Sufficiente
0
Nessun voto
Mediocre
0
Nessun voto
Scarso
0
Nessun voto
 
Voti totali : 0

Conversione e Validazione con JSF

Messaggioda alesslud » lun ott 12, 2009 12:54 pm

Illustriamo un esempio di conversione e validazione con JSF.
L'esempio mostra la conversione di una data, di un numero, di un Range relativo ad un Long, di un campo obbligatorio ecc.
Allegati
facesconverter.zip
L'esempio è stato provato con Eclipse Galileo
(9.29 KiB) Scaricato 337 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » lun ott 12, 2009 12:55 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>
<h1><h:outputText value="#{res.enter}" /></h1>
<h:panelGrid columns="3">
<h:outputText value="#{res.amount}" />
<h:inputText id="amount" value="#{myBean.importo}">
<f:convertNumber minFractionDigits="2" />
</h:inputText>
<h:message for="amount" styleClass="errorMessage" />

<h:outputText value="#{res.creditCard}" />
<h:inputText id="card" value="#{myBean.creditCard}">
<f:validateLength minimum="5" />
</h:inputText>
<h:panelGroup />

<h:outputText value="#{res.expirationDate}" />
<h:inputText id="date" value="#{myBean.data}"
converterMessage="Data non valida {0}">
<f:convertDateTime pattern="MM/yyyy" />
</h:inputText>
<h:message for="date" styleClass="errorMessage" />
<h:outputText value="#{res.longValidation}" />
<h:inputText id="longRange" value="#{myBean.longRange}"
converterMessage="Range superato" label="ciao">
<f:validateLongRange minimum="10" maximum="10000" />
</h:inputText>
<h:message showSummary="true" showDetail="false" for="longRange" styleClass="errorMessage" />

<h:outputText value="#{res.inputRequired}" />
<h:inputText id="inputRequired" value="#{myBean.inputRequired}"
converterMessage="Campo obbligatorio" required="true">
</h:inputText>
<h:message for="inputRequired" styleClass="errorMessage" />
</h:panelGrid>
<h:commandButton value="#{res.process}" action="process" />
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

result.jsp

Messaggioda alesslud » lun ott 12, 2009 12:55 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>
<h1><h:outputText value="#{res.title}"/></h1>
<h:panelGrid columns="2">
<h:outputText value="#{res.amount}"/>
<h:outputText value="#{myBean.importo}">
<f:convertNumber type="currency"/>
</h:outputText>

<h:outputText value="#{res.creditCard}"/>
<h:outputText value="#{myBean.creditCard}"/>

<h:outputText value="#{res.expirationDate}"/>
<h:outputText value="#{myBean.data}">
<f:convertDateTime pattern="MM/yyyy"/>
</h:outputText>

<h:outputText value="#{myBean.longRange}"/>
<br />
<h:outputText value="#{myBean.inputRequired}"/>
</h:panelGrid>
<h:commandButton value="Back" action="back"/>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

style.css

Messaggioda alesslud » lun ott 12, 2009 12:56 pm

.errorMessage {
color: red;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » lun ott 12, 2009 12:56 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 » lun ott 12, 2009 12:56 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>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>process</from-outcome>
<to-view-id>/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<navigation-rule>
<from-view-id>/result.jsp</from-view-id>
<navigation-case>
<from-outcome>back</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.aquilasiti.MyBean</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 » lun ott 12, 2009 12:57 pm

package com.aquilasiti;

import java.util.Date;

public class MyBean {
private double importo;
private String creditCard = "";
private Date data = new Date();
private Long longRange;
private String inputRequired;

public String getInputRequired() {
return inputRequired;
}

public void setInputRequired(String inputRequired) {
this.inputRequired = inputRequired;
}

public Long getLongRange() {
return longRange;
}

public void setLongRange(Long longRange) {
this.longRange = longRange;
}

public double getImporto() {
return importo;
}

public void setImporto(double importo) {
this.importo = importo;
}

public String getCreditCard() {
return creditCard;
}

public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}

public Date getData() {
return data;
}

public void setData(Date data) {
this.data = data;
}

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

messages.properties

Messaggioda alesslud » lun ott 12, 2009 12:57 pm

title=Test sulla Formattazione
enter=Inserisci le informazioni richieste per eseguire il test
amount=Imprto:
creditCard=Carta di Credito
expirationDate=Data (Month/Year):
process=Process
title=Test di f:
longValidation=Long Validation
inputRequired=Campo obbligatorio
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 0 ospiti

cron