2

I am working with JSF 2.2 and I would like the next alert message (picture) don't show when user selected the "Seleccione..."message:

enter image description here

[serverERROR: class javax.faces.component.UpdateModelException Cannot convert Seleccione... of type class java.lang.String to class modelo.entidades.Equipo]

My Bean is:

@ManagedBean
@SessionScoped
public class Bean{

    private String message;
    private Equipo equipoSelected;
    private List<Equipo> equipos;

    public Bean() {
    }

    @PostConstruct
    public void init(){
        message = "Seleccione...";
        equipos = new ArrayList<Equipo>();
        equipos.add(new Equipo(/*....*/));
        equipos.add(new Equipo(/*....*/));
        equipos.add(new Equipo(/*....*/));
        equipoSelected = new Equipo();
    }

    public void updateSelected(){
        //...
    }

    public setMessage(String message){
        this.message = message;
    }

    public String getMessage(){
        return message;
    }

    public void setEquipos(List<Equipo> equipos){
        this.equipos = equipos;
    }

    public List<Equipo> getEquipos(){
        return equipos;
    }
    public void setEquipoSelected(Equipo equipoSelected){
        this.equipoSelected = equipoSelected;
    }

    public Equipo getEquipoSelected(){
        return equipoSelected;
    }



}

and the xhtml code is:

<h:form>
    <h:selectOneMenu value="#{bean.equipoSelected}">
        <f:selectItem itemValue = "#{null}" itemLabel="#{bean.message}"/>
        <f:selectItems value="#{bean.equipos}" var="equipo" itemLabel="#{equipo.name}" itemValue="#{equipo}"/>
        <f:ajax event="change" listener="#{bean.updateSelected}" render="@form" execute="@form"/>
    </h:selectOneMenu>
</h:form>

thank you!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Juan C
  • 89
  • 7

2 Answers2

0

You're using objects of type Equipo in the Java bean, but the JSF component only copes with strings. You can fix this using a converter. Here's a good explanation of converters in general. This StackOverflow question has examples specifically for JSF (both with and without PrimeFaces).

You may also want to adopt the converter of OmniFaces. That's adding another library, but after that, the converter "automagically" does everything for you with almost no extra effort on your side.

Community
  • 1
  • 1
Stephan Rauh
  • 3,069
  • 2
  • 18
  • 37
0

Try with add attribute noSelectionOption="true" in tag f:selectItem.

These other question maybe help you: Best way to add a "nothing selected" option to a selectOneMenu in JSF

Community
  • 1
  • 1