2013-07-07 99 views
1

在我的xhtml页面中,我有两个依赖selectOneMenu,第二个填充ajax调用。下面是JSF的代码片段:p:selectOneMenu值仍然需要

<h:panelGrid columns="2" cellpadding="5"> 
    <h:outputLabel value="Dirección:" for="direccion"/> 
     <p:inputText id="direccion" value="#{datosInstitucion.institucion.direccion}" required="true" label="Dirección"/> 
     <h:outputLabel value="Departamento:" for="departamento"/> 
     <p:selectOneMenu id="departamento" value="#{datosInstitucion.idDepartamento}" required="true" label="Departamento"> 
      <f:selectItem itemLabel="Seleccione el departamento" itemValue="#{null}"/> 
      <c:forEach items="#{datosInstitucion.departamentos}" var="departamento"> 
      <f:selectItem itemLabel="#{departamento.nombre}" itemValue="#{departamento.id}"/> 
      </c:forEach> 
      <f:ajax render="municipio" listener="#{datosInstitucion.cargarMunicipios()}"/> 
     </p:selectOneMenu> 
     <h:outputLabel value="Municipio:" for="municipio"/> 
     <p:selectOneMenu id="municipio" value="#{datosInstitucion.idMunicipio}"           required="true" label="Municipio"> 
      <f:selectItem itemLabel="Seleccione el municipio" itemValue="#{null}"/> 
      <c:forEach items="#{datosInstitucion.municipios}" var="municipio"> 
       <f:selectItem itemLabel="#{municipio.nombre}" itemValue="#{municipio.id}"/> 
      </c:forEach> 
     </p:selectOneMenu> 
    </h:panelGrid> 

该代码片段是primefaces向导组件内,因此,当“下一个”按钮被按下一个验证错误被用于即使当有一个值组中的第二selectOneMenu用于引起。

什么可能导致此行为?

相关支持bean代码:

@ManagedBean(name = "datosInstitucion") 
@ViewScoped 
public class DatosInstitucion implements Serializable{ 

    @EJB 
    private Instituciones instituciones; 

    @EJB 
    private Parametros parametros; 

    @Inject 
    private Mensajes mensajes; 

    private List<Departamento> departamentos; 
    private List<Municipio> municipios; 
    private Map<Integer, Departamento> mapaDepartamentos; 
    private Integer idDepartamento; 
    private Integer idMunicipio; 

    private Institucion institucion; 

    @PostConstruct 
    private void inicializar(){ 
     this.mapaDepartamentos = new HashMap<>(); 
     this.departamentos = parametros.consultarDepartamentos(); 
     for(Departamento departamento : departamentos){ 
      this.mapaDepartamentos.put(departamento.getId(), departamento); 
     } 
     this.prepararInstitucion(); 
    } 

    private void prepararInstitucion(){ 
     this.institucion = new Institucion(); 
     this.institucion.setResponsable(new Persona()); 
    } 

    public Institucion getInstitucion() { 
     return institucion; 
    } 

    public List<Departamento> getDepartamentos(){ 
     return departamentos; 
    } 

    public TipoIdentificacion[] getTiposIdentificacion(){ 
     return TipoIdentificacion.deResponsables(); 
    } 

    public Integer getIdDepartamento() { 
     return idDepartamento; 
    } 

    public void setIdDepartamento(Integer idDepartamento) { 
     this.idDepartamento = idDepartamento; 
    } 

    public Integer getIdMunicipio() { 
     return idMunicipio; 
    } 

    public void setIdMunicipio(Integer idMunicipio) { 
     this.idMunicipio = idMunicipio; 
    } 

    public void cargarMunicipios(){ 
     idMunicipio = null; 
     if(idDepartamento != null){ 
      this.municipios = mapaDepartamentos.get(idDepartamento).getMunicipios(); 
     }else{ 
      this.municipios = Collections.emptyList(); 
     } 
    } 

    public List<Municipio> getMunicipios() { 
     return municipios; 
    } 

    public void confirmar(){ 
     this.instituciones.guardar(institucion); 
     this.mensajes.exito("La institución ha sido registrada en el sistema"); 
     this.prepararInstitucion(); 
    } 
} 
+1

使用''而不是JSTL'' – fareed

+0

@ ed Yes是的,将替换为解决了问题。谢谢! – fturizo

+0

欢迎您:) – fareed

回答

1

这是因为你使用JSTL <c:foreach>与JSF。 JSTL与JSF的生命周期很重要。 JSTL在构建视图时执行,而JSF在视图呈现时执行。两者不能相互同步。在你的情况,你需要使用<f:selectItems>代替<c:foreach>

替换:

<c:forEach items="#{datosInstitucion.municipios}" var="municipio"> 
     <f:selectItem itemLabel="#{municipio.nombre}" itemValue="#{municipio.id}"/> 
</c:forEach> 

有:

<f:selectItems value="#{datosInstitucion.municipios}" 
    var="municipio" itemLabel="#{municipio.nombre}" 
    itemValue="#{municipio.id}"/> 

更多的阅读,我建议你阅读以下answer

相关问题