ChrisF
ChrisF

Reputation: 25

h:selectOneMenu - losing value in JSP when commandAction clicked?

I am using a <h:selectOneMenu> to send a state value to the backing bean. When I click on the <h:commandLink> the value is being picked up in the bean correctly (in the log.debug message). However, when the page reloads, the selected State is lost and the one at the top (NY) appears in the UI. Shouldn't the value of the one selected be retained? Any suggestions much appreciated.

I am using JSF 1.2.

JSP:

<h:selectOneMenu id="state" value="#{stateBean.stateName}"> 
    <f:selectItem itemValue="NY" itemLabel="New York" />
    <f:selectItem itemValue="CA" itemLabel="California" />
    <f:selectItem itemValue="NE" itemLabel="Nebraska" />
    <f:selectItem itemValue="AK" itemLabel="Alaska" />
</h:selectOneMenu>

<h:commandLink action="#{stateBean.sendStateAction}">

Managed Bean:

private String stateName;

log.debug("state name: " + stateName);

public String getStateName() {
    return stateName;
}
public void setStateName(String name) {
    this.stateName = name;
}

Upvotes: 0

Views: 554

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Probably your managed bean is configured to be as request scope. If you change it to session scope the bean will be available while the user session is still alive. Since JSF 1.2 doesn't have a view scope, you could try to save the data in session and recover it in the constructor of your bean or use a third party library that handles this. More info: JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view? I have used RichFaces @KeepAlive and worked for my work.

Upvotes: 1

Related Questions