PhaseListener: utilizzarlo in un Logger

Forum relativo ai framework per l'utilizzo della tecnologia JSP

PhaseListener: utilizzarlo in un Logger

Messaggioda alesslud » mar nov 03, 2009 1:56 pm

Un esempio di un tracciatore di Fasi che implementa l'interfaccia PhaseListener.

L'esempio mostra il risultato nel output del vostro server.
Allegati
faceslisteners.zip
Il progetto è stato sviluppato con Eclipse Galileo e testato con JBoss 5.0.
(11.32 KiB) Scaricato 276 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » mar nov 03, 2009 1:56 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="prop"/>
<title>
<h:outputText value="#{prop.title}"/>
</title>
</head>

<body>
<h:form>
<h:panelGrid columns="2" columnClasses="faseColumns">
<h:outputText value="#{prop.faseText}"/>

<h:selectOneMenu valueChangeListener="#{form.cambioFase}">
<f:selectItems value="#{form.fasi}"/>
</h:selectOneMenu>

<h:commandButton value="#{prop.submit}"/>
</h:panelGrid>
</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 nov 03, 2009 1:57 pm

body {
background: silver;
}
.faseColumns {
vertical-align: top;
font-style: italic;
font-size: medium;
}
.columns {
vertical-align: top;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » mar nov 03, 2009 1:57 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>
<context-param>
<param-name>com.corejsf.phaseTracker.phase</param-name>
<param-value>ANY_PHASE</param-value>
</context-param>

<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 nov 03, 2009 1:58 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>form</managed-bean-name>
<managed-bean-class>com.aquilasiti.MyBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<!-- qui viene configurato il Tracker, cioè il tracciatore che ti dice la fase selezionata -->
<!-- utile per il log -->
<lifecycle>
<phase-listener>com.aquilasiti.FaseTracker</phase-listener>
</lifecycle>
</faces-config>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages.properties

Messaggioda alesslud » mar nov 03, 2009 1:58 pm

title=Eventi relativi ad una Phase
faseText=Seleziona una fase:
submit=Invia
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

MyBean.java

Messaggioda alesslud » mar nov 03, 2009 1:58 pm

package com.aquilasiti;

import javax.faces.FactoryFinder;
import javax.faces.event.PhaseListener;
import javax.faces.event.ValueChangeEvent;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.faces.model.SelectItem;

public class MyBean {
private SelectItem[] fasi = {
new SelectItem("RESTORE_VIEW"),
new SelectItem("APPLY_REQUEST_VALUES"),
new SelectItem("PROCESS_VALIDATIONS"),
new SelectItem("UPDATE_MODEL_VALUES"),
new SelectItem("INVOKE_APPLICATION"),
new SelectItem("RENDER_RESPONSE"),
new SelectItem("ANY_PHASE"),
};

public SelectItem[] getFasi() { return fasi; }

public void cambioFase(ValueChangeEvent e) {
LifecycleFactory factory = (LifecycleFactory)FactoryFinder.getFactory(
FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = factory.getLifecycle(LifecycleFactory.
DEFAULT_LIFECYCLE);

PhaseListener[] listeners = lifecycle.getPhaseListeners();
for (int i = 0; i < listeners.length; ++i) {
PhaseListener listener = listeners[i];
if(listener instanceof com.aquilasiti.FaseTracker)
((com.aquilasiti.FaseTracker) listener).setPhase(
(String) e.getNewValue());
}
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

FaseTracker.java

Messaggioda alesslud » mar nov 03, 2009 1:59 pm

package com.aquilasiti;

import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseListener;
import javax.faces.event.PhaseId;

public class FaseTracker implements PhaseListener {
/**
*
*/
private static final long serialVersionUID = -3496502831872418150L;

private static final String PHASE_PARAMETER = "com.aquilasiti.fasiTracker.fase";
//TODO: creare il logger
private static final Logger logger = Logger.getLogger("com.aquilasiti.fasi");
private static String fase = null;

public void setPhase(String newValue) {
fase = newValue;
}

@Override
public PhaseId getPhaseId() {
PhaseId phaseId = null;
try {
if (fase == null) {
FacesContext context = FacesContext.getCurrentInstance();
fase = (String) context.getExternalContext().getInitParameter(
PHASE_PARAMETER);
}
phaseId = PhaseId.ANY_PHASE;

if (fase != null) {
if ("RESTORE_VIEW".equals(fase))
phaseId = PhaseId.RESTORE_VIEW;
else if ("APPLY_REQUEST_VALUES".equals(fase))
phaseId = PhaseId.APPLY_REQUEST_VALUES;
else if ("PROCESS_VALIDATIONS".equals(fase))
phaseId = PhaseId.PROCESS_VALIDATIONS;
else if ("UPDATE_MODEL_VALUES".equals(fase))
phaseId = PhaseId.UPDATE_MODEL_VALUES;
else if ("INVOKE_APPLICATION".equals(fase))
phaseId = PhaseId.INVOKE_APPLICATION;
else if ("RENDER_RESPONSE".equals(fase))
phaseId = PhaseId.RENDER_RESPONSE;
else if ("ANY_PHASE".equals(fase))
phaseId = PhaseId.ANY_PHASE;
}
return phaseId;
} catch (Exception e) {
e.printStackTrace();
}
return phaseId;
}

@Override
public void beforePhase(PhaseEvent e) {
logger.info("BEFORE " + e.getPhaseId());
}

@Override
public void afterPhase(PhaseEvent e) {
logger.info("AFTER " + e.getPhaseId());
}
}
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