1

私のxhtmlページには、依存するselectOneMenuが2つあり、2つ目はajax呼び出しで埋められています。jsf コードの一部を次に示します。

   <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 ウィザード コンポーネント内にあるため、「次へ」ボタンが押されると、値が設定されている場合でも、2 番目の 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();
    }
}
4

1 に答える 1

1

これは<c:foreach>、JSF で JSTL を使用しているためです。JSTL と JSF のライフサイクルが重要です。JSTL はビューの作成時に実行され、JSF はビューのレンダリング時に実行されます。この 2 つは互いに同期して動作しません。あなたの場合、<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}"/>

さらに読むには、次の回答を読むことをお勧めします

于 2013-07-08T05:45:13.467 に答える