AdSsa
AdSsa

Reputation: 288

<h:selectOneMenu> doesn't save on the bean the value selected

I have problems with SelectOneMenu. I write this:

<h:selectOneMenu id="listaEstados"
        styleClass="comboboxStyle" 
        value="#{detalleSistemaBean.sistema.indEstado}" 
        immediate="true">
    <f:selectItems value="#{detalleSistemaBean.indEstados}" />
</h:selectOneMenu>
<h:commandButton id ="SubmitModificar"
    value="Modificar"
    styleClass="botonPeque"
    action="#{detalleSistemaBean.modificaSistema}">
</h:commandButton>  

But when I choose one value from the list "indEstados" and I submit the form, the bean "sistema.indEstado" doesn't change. I have seen that the bean property changes just before the method modificaSistema, but inside this method (where I have a database connection and a sql sentence), "sistema.indEstado" returns to its original value. Why this happens? I have tried to save the value using valueChangeListener, and that works, but I guess that is not a neat solution.

Upvotes: 1

Views: 2533

Answers (2)

BalusC
BalusC

Reputation: 1108642

That can happen when you're doing data loading inside the getter method instead of inside the (post)constructor of the bean class.

Fix your managed bean code to not do anything else inside getter methods than just returning the property.

I.e. do not do

public Sistema getSistema() {
    return sistemaService.find(someSistemaId);
}

but rather do

private Sistema sistema;

@PostConstruct
public void init() {
    sistema = sistemaService.find(someSistemaId);
}

public Sistema getSistema() {
    return sistema;
}

Upvotes: 2

Abhi
Abhi

Reputation: 6568

Can you try without setting

 immediate="true"

JSF commandButton with immediate="true"

Upvotes: 1

Related Questions