p:selectOneMenu does not set value in bean
Asked Answered
S

4

17

Quick question. This hasn't happened to me before when working with SelectOneMenu. This is my code.

<h:outputLabel for="listaRegiones" value="Región: " />
<p:selectOneMenu id="listaRegiones" value="#{nuevaProvincia.regionSelect}" required="true">
    <f:selectItems value="#{nuevaProvincia.regiones}" />
</p:selectOneMenu>
<p:message for="listaRegiones" />

And this is my backing bean.

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

    public static final long serialVersionUID = 1L;

    public nuevaProvincia() throws DAOException {
        this.provincia = new Provincia();
        this.regiones = new ArrayList<SelectItem>();
        ArrayList<Region> regs = new ArrayList<Region>();
        try
        {
            regs = Region.obtenerRegiones();
        }
        catch(DAOException e)
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Hubo un error: "+e.getMessage(), "Hubo un error: "+e.getMessage()));
        }
        if(regs.size()>0)
        {
            for(Region r : regs)
            {
                SelectItem item = new SelectItem(r.getCodigo(), r.getNombre());
                regiones.add(item);
            }
            this.regionSelect = regs.get(0).getCodigo();
        }
        else
            this.regionSelect = "";
    }

    public void verificaProvincia() throws DAOException {
        provincia.getRegion().setCodigo(regionSelect);
        try
        {
            if(this.provincia.estaCreado())
                FacesContext.getCurrentInstance().addMessage("frmIngProvincia:provCodigo", new FacesMessage(FacesMessage.SEVERITY_WARN, "El código de provincia ingresado ya existe.", "El código de provincia ingresado ya existe."));
            else
                FacesContext.getCurrentInstance().addMessage("frmIngProvincia:provCodigo", new FacesMessage(FacesMessage.SEVERITY_INFO, "El código de provincia ingresado no existe.", "El código de provincia ingresado no existe."));
        }
        catch(DAOException e)
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Hubo un error: "+e.getMessage(), "Hubo un error: "+e.getMessage()));
        }
    }

    public void insertaProvincia() throws DAOException {
        try
        {
            provincia.getRegion().setCodigo(regionSelect);
            provincia.guardar();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Provincia ingresada con éxito", "Provincia ingresada con éxito"));
        }
        catch(DAOException e)
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Hubo un error: "+e.getMessage(), "Hubo un error: "+e.getMessage()));
            throw e;
        }
    }

    //Getters and setters for everything        

    //Privados
    private Provincia provincia;
    private String regionSelect;
    private List<SelectItem> regiones;
}

The problem is as follows: whenever I change the value in my selectonemenu, the value in the backing bean is not being set (although I do have a setter for regionSelect). Why would this happen?

Sentinel answered 10/11, 2012 at 22:28 Comment(5)
If your line SelectItem item = new SelectItem(r.getCodigo(), r.getNombre()); is giving something else than String as ItemValue, you will get a bad value error (you can try using <h:messages globalOnly="false" /> to see if something is going wrong)Overturf
Both values set in SelectItem are strings, and <h:messages globalOnly="false" /> is not telling me anything new.Sentinel
Have you other fields in this form? If yes, does other fields are updated?Overturf
I just tested one field and yes, it gets updated correctly.Sentinel
Wha ... I didn't really make any change and it's working. Thank you.Sentinel
S
29

I found the error and it was ... quite strange and circumstantial. I added the following line inside the SelectOneMenu:

<p:ajax event="change" update="@this" />

and now it works just fine.

Sentinel answered 11/11, 2012 at 0:58 Comment(6)
Excellent, I'm still using RichFaces so I think there are some differences!Overturf
It's possible both are quite different on their behaviors!Sentinel
This is the expected behavior. Your example will set the selectOneMenu value on form submit. If you want to set it with ajax, you have to specify it. Please mark your post as answer.Erelong
I'm going to mark it as my answer, only that I must wait until tomorrow. Also, does it mean I should place a p:ajax in every SelectOneMenu I want to verify during runtime?Sentinel
I have the same case with inputTexfield, do I need to use ajax for the same ?Coconut
I've not used JSF for ages so I kinda forgot, but iirc, it shouldn't apply given it's just an inputTextField rather than a selectOneMenu. Try using it though and see if it works. Or maybe your field is not mapped to the bean? I don't know anymore!Sentinel
C
1
<p:selectOneMenu id="listaRegiones" value="#{nuevaProvincia.regionSelect}" required="true">
    <f:selectItems value="#{nuevaProvincia.regiones}" />
</p:selectOneMenu>

should be

<p:selectOneMenu id="listaRegiones" value="#{nuevaProvincia.regionSelect}" required="true">
    <f:selectItems value="#{nuevaProvincia.regiones}" var="region" 
      itemValue = "#{region}"/>
</p:selectOneMenu>

That's why you have to have the ajax call to update whenever there is a change. You are never setting the value.

Conterminous answered 12/1, 2014 at 11:3 Comment(1)
Does var="region" itemValue="#{region}" do that was questioned?Myall
S
0

If you are working objects in the value, check the equals() function.

Summation answered 11/3, 2019 at 3:1 Comment(0)
T
-1

I discovered when I had this problem it was because of the @ViewScoped. Using SessionScoped instead worked fine.

One interesting thing is that in another project, with ViewScoped, it worked. I cannot explain.

Tonitonia answered 10/12, 2015 at 17:17 Comment(1)
You most likely used the wrong combination of @ViewScoped and @Named/@ManagedBean Annotation. If you would have taken the wrong @SessionScoped it would have failed to...Brezhnev

© 2022 - 2024 — McMap. All rights reserved.