JSF selectOneMenu is updated and returns to its previous state, instead of showing a new value

I have a datatable where many selectOneMenu elements are available, for example, for 10 elements, each of which has one selectOneMenu command. Now, if I click on any of the combos, they should save the value in the database, and they will do it. but after saving the changed value, selectOneMenu returns to its previous state. I want selectOneMenu to keep current state. In addition, this method is called for each individual combo in a datatable. I really wonder why! I hit my head in the last 2 weeks. Any help could be helpful. thanks in advance.

This is my first post here. this is my jsf datatable:

<h:dataTable value="#{careNeedBean.controlledCareNeedsList}" var="careNeed" id="careneed_table" binding="#{careNeedBean.dataTable}"> <h:column> <f:facet name="header"> <h:outputText value="NeedsLevel"/> </f:facet> <h:selectOneMenu id="needs_level_combo" style="width:200px;font-size:9px;" onchange="submit()" valueChangeListener="#{careNeedBean.saveTaskAsessment}" binding="#{careNeedBean.selectOneMenu}"> <f:selectItem itemValue="not_assessed" itemLabel="----Not assessed----"/> <f:selectItems value="#{careNeed.humanReadableNeedsList}" /> </h:selectOneMenu> </h:column> 

This is my bean code:

 public String saveTaskAsessment(ValueChangeEvent event) { //does some things return "Success"; } 
+4
source share
2 answers

valueChangeListener not executed only for a newly changed component. In fact, you use the JavaScript submit() function to submit the entire form. valueChangeListener will always be executed whenever the new selected value is different from the old value, as was declared in the value attribute.

You have not declared the value attribute, so its default value is effectively null . If the default list item is not null , then valueChangeListener will be called.

To fix this, you need to assign a value attribute to the component

 <h:selectOneMenu value="#{careNeed.needsLevel}"> 

and , you need to pre-populate it with the same value as the default value for the drop-down list.

 this.needsLevel = "not_assessed"; 

Alternatively, you can also make the default value null .

 <f:selectItem itemValue="${null}" itemLabel="----Not assessed----"/> 

Unrelated to the problem, since you are already using JSF 2.0, I suggest using <f:ajax> to send only the recently modified drop-down list using ajaxical powers instead of using onchange="submit()" to submit the entire form. This is better for users.

 <h:selectOneMenu> <f:ajax /> </h:selectOneMenu> 

In addition, the valueChangeListener method valueChangeListener not need to return anything. In any case, this will be ignored. Just declare it void .

+2
source

You can use AjaxSingle = "true" and onsubmit = "form.refresh ();" at your request ajax.

So it will only process the current component. form.refresh (); will remove the old cache value. You will get the updated bean value.

+1
source

Source: https://habr.com/ru/post/1336303/


All Articles