Problems with p: selectManyCheckbox using Enum

0

Good evening, I'm doing a registration page but I came across a problem with p:selectManyCheckbox

<p:outputLabel value="Nome" for="nomepa" />
<p:autoComplete id="nomepa" size="40" dropdown="true"
    value="#{cadastroListaBean.espera.paciente}"
    completeMethod="#{cadastroListaBean.completarPaciente}"
    var="paciente" itemLabel="#{paciente.nome}" itemValue="#{paciente}"
    forceSelection="true">
    <p:ajax event="itemSelect" process="@this"
        update="frmCadastroLista"
        listener="#{cadastroListaBean.completarCampo}" />
</p:autoComplete>

<p:outputLabel value="Identificação" for="din" />
<h:outputText id="din" size="20" maxlength="30"
    value="#{cadastroListaBean.espera.paciente.identificacao}" />

<p:outputLabel value="Sexo" for="sex" styleClass="sexo" />
<h:outputText id="sex" size="20" maxlength="30"
    value="#{cadastroListaBean.espera.paciente.sexo.descricao}" />

<p:outputLabel value="Celular" for="cel" />
<h:outputText id="cel" maxlength="20"
    value="#{cadastroListaBean.espera.paciente.telefone}" />

<p:outputLabel value="Idade" for="idad" />
<h:outputText id="idad" size="10" maxlength="10"
    value="#{cadastroListaBean.espera.paciente.idade}" />


<p:outputLabel value="Urgência" for="urgencia" />
<p:selectManyCheckbox id="urgencia"
    value="#{cadastroListaBean.espera.urgencia}">
    <f:selectItems value="#{cadastroListaBean.urgencias}"
        var="urgencia" itemValue="#{urgencia}"
        itemLabel="#{urgencia.descricao}" />
</p:selectManyCheckbox>

My Bean

@Named
@ViewScoped
public class CadastroListaBean implements Serializable {
    private static final long serialVersionUID = 1L;

    private ListaEspera espera;

    public void salvar() {
            this.espera = cadastroListaService.salvar(this.espera);
    }


    public Urgencia[] getUrgencias(){
        return Urgencia.values();

    }

    public ListaEspera getEspera() {
        return espera;
    }

    public void setEspera(ListaEspera espera) {
        this.espera = espera;
    }

}

The error that appears is:

Caused by: javax.faces.FacesException: Target model Type is no a Collection or Array
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:391)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:128)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:314)
    at org.primefaces.component.selectmanycheckbox.SelectManyCheckboxRenderer.getConvertedValue(SelectManyCheckboxRenderer.java:39)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1034)
    at javax.faces.component.UIInput.validate(UIInput.java:964)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1237)
    at javax.faces.component.UIInput.processValidators(UIInput.java:702)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1164)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    ... 53 more

Do you think that was what?

The page:

                                                      

            <p:outputLabel value="Identificação" for="din" />
            <h:outputText id="din" size="20" maxlength="30"
                value="#{cadastroListaBean.espera.paciente.identificacao}" />

            <p:outputLabel value="Sexo" for="sex" styleClass="sexo" />
            <h:outputText id="sex" size="20" maxlength="30"
                value="#{cadastroListaBean.espera.paciente.sexo.descricao}" />

            <p:outputLabel value="Celular" for="cel" />
            <h:outputText id="cel" maxlength="20"
                value="#{cadastroListaBean.espera.paciente.telefone}" />

            <p:outputLabel value="Idade" for="idad" />
            <h:outputText id="idad" size="10" maxlength="10"
                value="#{cadastroListaBean.espera.paciente.idade}" />


            <p:outputLabel value="Urgência" for="urgencia" />
            <p:selectManyCheckbox id="urgencia"
                value="#{cadastroListaBean.espera.urgencia}"
                converter="urgenciaConverter">
                <f:selectItems value="#{cadastroListaBean.urgencias}"
                    var="urgencia" itemValue="#{urgencia}"
                    itemLabel="#{urgencia.descricao}" />
                <f:attribute name="collectionType" value="java.util.ArrayList" />
            </p:selectManyCheckbox>

My bean

@Named @ViewScoped public class RegistrationListBean implements Serializable {

private static final long serialVersionUID = 1L;

private ListaEspera espera;

@Inject
private Pacientes pacientes;
@Inject
private CadastroListaService cadastroListaService;

public CadastroListaBean() {
    limpar();
}

private void limpar() {
    espera = new ListaEspera();

}

public List<Paciente> completarPaciente(String nome) {
    return this.pacientes.porNome(nome);

}

public void completarCampo() {
    this.espera.completarCampo();
}

public void salvar() {
    try {
        this.espera = cadastroListaService.salvar(this.espera);
        limpar();
        FacesUtil.addInfoMessage("Inclusão na lista de espera com sucesso!");
    } finally {

    }
}

public void inicializar() {
    if (FacesUtil.isNotPostback()) {

        this.espera.completarCampo();

    }

}

public List<SelectItem> getUrgencias() {
    List<SelectItem> listUrgencias = new ArrayList<SelectItem>();
    for (Urgencia ug : Urgencia.values()) {
        listUrgencias.add(new SelectItem(ug, ug.getDescricao()));
    }
    return listUrgencias;
}

public ListaEspera getEspera() {
    return espera;
}

public void setEspera(ListaEspera espera) {
    this.espera = espera;
}

}

o Converter:

@FacesConverter (value="urgencyConverter") public class UrgencyConvert implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value != null) {
        return Urgencia.valueOf(value);
    }

    return null;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null && value instanceof Urgencia) {
        return ((Urgencia) value).name();
    }
    return null;
}

}

o Error:

  

Caused by: javax.faces.FacesException: Target model Type is not a   Collection or Array at   com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel (MenuRenderer.java:391)     at   com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue (MenuRenderer.java:128)     at   com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue (MenuRenderer.java:314)     at   org.private.config     at javax.faces.component.UIInput.getConvertedValue (UIInput.java:1034)     at javax.faces.component.UIInput.validate (UIInput.java:964) at   javax.faces.component.UIInput.executeValidate (UIInput.java:1237) at   javax.faces.component.UIInput.processValidators (UIInput.java:702) at   javax.faces.component.UIComponentBase.processValidators (UIComponentBase.java:1220)     at javax.faces.component.UIForm.processValidators (UIForm.java:253)     at   javax.faces.component.UIComponentBase.processValidators (UIComponentBase.java:1220)     at   javax.faces.component.UIComponentBase.processValidators (UIComponentBase.java:1220)     at   javax.faces.component.UIViewRoot.processValidators (UIViewRoot.java:1164)     at   com.sun.faces.lifecycle.ProcessValidationsPhase.execute (ProcessValidationsPhase.java:76)     ... 53 more

    
asked by anonymous 18.09.2017 / 03:38

1 answer

2

Your element items must be a List It is recommended that you do a List

In your bean would be

public List<SelectItem> getUrgencias(){ 
  List<SelectItem> listUrgencias = new ArrayList<SelectItem>();
  for(Urgencia ug : Urgencia.values()){
    listUrgencias.add(new SelectItem(ug , ug.getDescricao()));
  }
  return listUrgencias;
}

And your component

 <p:selectManyCheckbox id="urgencia"
    value="#{cadastroListaBean.espera.urgencia}" converter="${urgenciaConverter}">
    <f:selectItems value="#{cadastroListaBean.urgencias}"
        var="urgencia" itemValue="#{urgencia}"
        itemLabel="#{urgencia.descricao}" />
 <f:attribute name="collectionType" value="java.util.ArrayList" />
</p:selectManyCheckbox>

Make a convert to your enum to import into your element

public class UrgenciaConverter implements Converter {  

@Override  
public Object getAsObject(FacesContext context, UIComponent component, String value) {  
    if (value != null) {  
        return Urgencia.valueOf(value);  
    }  

    return null;  
}

@Override  
public String getAsString(FacesContext context, UIComponent component, Object value) {  
    if (value != null && value instanceof Urgencia) {  
        return ((Urgencia) value).name();  
    }  
    return null;  
}  

}

    
22.09.2017 / 14:06