简体   繁体   中英

selectOneMenu does not change the value of the bean object

So I have the following code:

<h:inputText value = "#{listAllBookings.searchText}">
  <f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"/>
</h:inputText>             
<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
  <f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
  <f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>

And my printValues method:

public void printValues() {
    System.out.println("searchText:"  + searchText + " and selectedAttr: " + selectedAttr);
}

So as you can see the code above is pretty simple. The problem is that I can't change the value of the selectedAttr value. I already checked if I have the appropriate getter and setter methods. The value of the selectedAttr remains null, while the searchText value changes.

The current output looks like this:

产量

You did not specify a component for execution with your ajax requests. So only the textfield is executed.

Add an id to the selectOneMenu and execute it:

<h:inputText value="#{listAllBookings.searchText}">
  <f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"
  execute="@this selectSomething"/>
</h:inputText>             
<h:selectOneMenu id="selectSomething" value="#{listAllBookings.selectedAttr}">
  <f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
  <f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>

But you could also execute the surrounding form via execute="@form"

See also

try this:

<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
<f:ajax event="change" listener="#{listAllBookings.printValues()}" />
  <f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
  <f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM