2011-05-22 31 views
10

La página se genera correctamente con los valores apropiados en bean administrado, pero los eventos ajax en estas dos h: selectOneMenus no funcionan. El oyente no es llamado. Un error tiene que estar en algún lugar dentro de las etiquetas, pero no lo veo.El método de escucha f: ajax en h: selectOneMenu no se ejecuta

<f:view> 
    <h:form> 
     <h:messages /> 
     <h:panelGrid columns="3"> 

      <h:outputLabel value="Choose your faculty: *" for="faculties" /> 
      <h:selectOneMenu id="faculties" value="#{registrateStudent.selectedFaculty}" > 
       <f:ajax event="change" listener="#{registrateStudent.genSpecializations}" execute="faculties" render="specializations" />       
       <f:selectItems value="#{registrateStudent.listFaculty}" var="curFac" itemLabel="#{curFac.name}" itemValue="#{curFac}" /> 
      </h:selectOneMenu> 
      <h:message id="message_faculties" for="faculties" /> 

      <h:outputLabel value="Choose your specialization: *" for="specializations" /> 
      <h:selectOneMenu id="specializations" value="#{registrateStudent.selectedSpecialization}" > 
       <f:selectItems value="#{registrateStudent.listSpecialization}" var="curSpec" itemLabel="#{curSpec.name}" itemValue="#{curSpec}"/> 
      </h:selectOneMenu> 
      <h:message id="message_specializations" for="specializations" />      

Managed Bean:

@ManagedBean(name = "registrateStudent") 
@ViewScoped 
public class RegistrateStudent { 


    private Faculty selectedFaculty; 
    private List<Faculty> listFaculty; 
    private Specialization selectedSpecialization; 
    private List<Specialization> listSpecialization; 
    private boolean showSpecialization = false; 


    /** Creates a new instance of RegistrateStudent */ 
    public RegistrateStudent() { 
     users = new Users(); 
     System.out.println("poaposd1"); 
     student = new Student(); 
    } 

    @PostConstruct 
    public void init() { 
     listFaculty = ff.findAll(); 
     if (listFaculty != null) { 
      selectedFaculty = listFaculty.get(0); 
      listSpecialization = sf.findByFaculty(selectedFaculty.getIdFaculty()); 
      if (listSpecialization != null) { 
       selectedSpecialization = listSpecialization.get(0); 
      } 
      else {} 
     } else {} 
    } 

    public void genSpecializations(AjaxBehaviorEvent event) { 
     if (sf.findByFaculty(selectedFaculty.getIdFaculty()) != null) { 
      this.showSpecialization = true; 
     } else { 
      JsfUtil.addSuccessMessage("faculties", "We don't have specializations for such faculty"); 
     } 
    } 
} 

ACTUALIZACIÓN:

he descubierto algunas cosas interesantes:

<f:ajax> etiqueta no funciona en <h:link>, <h:selectOneMenu>, <h:button>, <h:commandButton>. En este caso, no se notan los valores incorrectos en el atributo render, pero el valor incorrecto del atributo event genera un error.

<h:outputLabel>, <h:inputText> trabajo con <f:ajax> adecuadamente

+0

¿Has intentado ejecutar = "@ this" en lugar de execute = "faculty"? –

+0

@George sí, intentó – kolobok

Respuesta

32

El <f:ajax> requiere que el archivo jsf.js se incluya en el HTML <head>. Contiene todas las funciones JS para hacer la magia JSF ajax.

Para lograr esto, asegúrese de estar usando <h:head> en lugar de <head> en la plantilla maestra. JSF incluirá automáticamente el elemento necesario <script> indicando jsf.js.

<!DOCTYPE html> 
<html lang="en" 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <h:head> 
     <title>Look, with h:head</title> 
    </h:head> 
    <h:body> 
     Put your content here. 
    </h:body> 
</html> 

Tenga en cuenta que en un navegador web decente bits con un conjunto de herramientas poco webdeveloper decente como el de Firefox Web Developer Toolbar y/o Firebug inmediatamente debe haber notado errores JS como jsf is undefined cuando la petición Ajax se va a ejecutar. Eso debería al menos haber dado algo en qué pensar.


actualización: según su actualización

he encontrado algunas cosas interesantes:

<f:ajax> etiqueta no funciona en <h:link>, <h:selectOneMenu>, <h:button>, <h:commandButton>. En este caso, no se notan los valores incorrectos en el atributo render, pero el valor incorrecto del atributo event genera un error.

<h:outputLabel>, <h:inputText> funcionan correctamente con <f:ajax>.

El <h:link> y <h:button> están destinados solo para solicitudes GET, no para solicitudes POST. Sin embargo, debería funcionar bien en <h:selectOneMenu> y <h:commandButton>. ¿No tiene más código en la imagen completa que omitió de la pregunta para simplificar? ¿Qué JSF impl/versión estás usando? ¿Estás utilizando las bibliotecas correctas en classpath? Parece que realmente debes haber estropeado algo.

Para convencer a usted (y yo) que acabo de crear la siguiente caso_prueba copy'n'paste'n'runnable

<!DOCTYPE html> 
<html lang="en" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
> 
    <h:head> 
     <title>SO question 6089924</title> 
    </h:head> 
    <h:body> 
     <h:form> 
      <h:selectOneMenu value="#{bean.selected}"> 
       <f:selectItem itemValue="#{null}" itemLabel="Select..." /> 
       <f:selectItem itemValue="one" /> 
       <f:selectItem itemValue="two" /> 
       <f:selectItem itemValue="three" /> 
       <f:ajax listener="#{bean.listener}" render="result" /> 
      </h:selectOneMenu> 

      <h:commandButton value="commandButton" action="#{bean.submit}"> 
       <f:ajax listener="#{bean.listener}" render="result" /> 
      </h:commandButton> 

      <h:outputText id="result" value="#{bean.selected} #{bean.result}" /> 

      <h:messages /> 
     </h:form> 
    </h:body> 
</html> 

con este frijol

package com.example; 

import java.io.Serializable; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.event.AjaxBehaviorEvent; 

@ManagedBean 
@ViewScoped 
public class Bean implements Serializable { 

    private String selected; 
    private String result; 

    public void submit() { 
     System.out.println("submit"); 
    } 

    public void listener(AjaxBehaviorEvent event) { 
     System.out.println("listener"); 
     result = "called by " + event.getComponent().getClass().getName(); 
    } 

    public String getSelected() { 
     return selected; 
    } 

    public void setSelected(String selected) { 
     this.selected = selected; 
    } 

    public String getResult() { 
     return result; 
    } 

} 

Funciona muy bien con Mojarra 2.1. 1 en Tomcat 7.0.12.

INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 
INFO: Initializing Mojarra 2.1.1 (FCS 20110408) for context '/playground' 
+0

gracias por ejemplo. supongo que 'value =" # {bean.selected} "' no se correlaciona con la propiedad de bean cuando muevo la etiqueta '' bajo '' debido a una solicitud POST? y por qué h: outputLabel no funciona sin h: form incluso si los elementos no están en este h: form? – kolobok

+0

Elementos de entrada HTML como '', '