本文介绍了p:selectOneMenu没有在bean中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问.在使用SelectOneMenu之前,这还没有发生在我身上.这是我的代码.

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" />

这是我的后援豆.

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

问题如下:每当我更改selectonemenu中的值时,都不会设置后备bean中的值(尽管我确实有一个regionSelect的设置器).为什么会这样?

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?

推荐答案

我发现了这个错误,它很奇怪而且很偶然.我在SelectOneMenu中添加了以下行:

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.

这篇关于p:selectOneMenu没有在bean中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 15:52