Ing. Eric Gustavo Coronel Castillo gcoronelc@gmail.com MANEJO DE EVENTOS 1 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo AGENDA Presentacin Value change events Action events Clase ValueChangeListener Clase ActionListener 2 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo Presentacin Los componentes UI generan eventos. Los listeners se implementan en backing beans o clases aparte. JSF soporta tres tipos de eventos: Value change events Disparados por elementos UIInput cuando el valor que contienen cambia. Action events Los disparan los elementos UICommand cuando el botn o enlace asociado es activado. Phase Events Son disparados rutinariamente por el ciclo de vida JSF.
3 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo Value Change Events <h:inputText value="#{demo01.texto1}" valueChangeListener="#{demo01.processValueChanged}"/>
4
public void processValueChanged(ValueChangeEvent event) {
} J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo DEMO 5 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo action event <h:form> . . . <h:commandButton value="procesar" actionListener="#{demo03.escuchar}" action="#{demo03.procesar()}"/> . . . </h:form> 6
@ManagedBean @RequestScoped public class Demo03 implements Serializable{ . . . public String procesar(){ . . . } public void escuchar(ActionEvent e){ . . . } } J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo DEMO 7 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo Clase ValueChangeListener La clase listener para los eventos de cambio de valor tiene que implementar una determinada interfaz: ValueChangeListener
public class CountryListener implements ValueChangeListener { public void processValueChange(ValueChangeEvent event) { ... } }
El controlador invoca el mtodo processValueChange tras cada cambio. 8 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo Clase ValueChangeListener Articulo: <h:selectOneMenu value="#{demo02.idart}" onchange="submit()"> <f:valueChangeListener type="jsf.bean.ProductoValueListener" /> <f:selectItem itemValue="A00" itemLabel="Seleccione producto" /> <f:selectItem itemValue="A01" itemLabel="producto 01" /> . . . </h:selectOneMenu>
9 public class ProductoValueListener implements ValueChangeListener {
@Override public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
}
} J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo DEMO 10 J a v a S e r v e r F a c e s Ing. Eric Gustavo Coronel Castillo Clase ActionListener Similar a la anterior: