Problems with p:selectManyCheckbox using Enum

Asked

Viewed 419 times

0

Good evening, I’m making a registration page but I came across a problem on 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

You think it 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 Cadastrolistabean Serializable Implements {

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;
}

}

the Convert:

@Facesconverter(value = "urgenciaConverter") 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;
}

}

the Error:

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

1 answer

2

Your widget items need to be a List The recommended is you make a List

In your bean would stay

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 its 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;  
}  

}

  • This is Mario. How’s it going? Sorry it takes so long to give an answer about how it works... The problem continued 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) .....

  • Even mundane Urgency[] getUrgencies() to List<Selectitem> and creating the convert ?

  • Good evening, I put everything you said... <p:selectManyCheckbox id="urgency" value="#{cadasListaBean.espera.urgencia}" converter="#{urgenciaConverter}"> <f:selectItems value="#{cadastroListaBean.Urgencias}" var="urgency" itemValue="#{urgency}" itemLabel="#{urgencia.Descricao}" /> <f:attribute name="collectionType" value="java.util.Arraylist" /> </p:selectManyCheckbox>

  • and in the converter @Named public class Urgencyconvert Implements Converter { , but keeps giving the same error. Voce think it can be oq? Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.