问题
I have a JSF 2.2 form that contains one <h:selectOneMenu>
, inputs and 4 buttons to call CRUD methods. In order to achieve the binding, I used a valueChangeListener
so that when I chose an id
from the <h:selectOneMenu>
, I call a method which updates the object linked to the inputs. The problem is that the inputs don't change their value.
The form
<h:selectOneMenu value="#{avocatBurController.numProf}"
valueChangeListener="#{avocatBurController.handelValueCahnge}" onchange="submit()">
<f:selectItems value="#{avocatBurController.lstAvocatbureau}" var="avcBur"
itemValue="#{avcBur.avocat.numProf}" itemLabel="#{avcBur.avocat.nom}" />
</h:selectOneMenu>
<h:inputText value="#{avocatBurController.avc.prenom}" />
<h:inputText value="#{avocatBurController.avc.nom}" />
...
ManagedBean
public void handelValueCahnge(ValueChangeEvent event) {
String numProf = (String) event.getNewValue();
AvocatBurDao avcBurDao = new AvocatBurDao();
avcBur = avcBurDao.getAvocatBur(numProf);
avc = avcBur.getAvocat();
System.out.println(avc.getNom());
}
avc.getNom()
contains a value but the inputs don't.
回答1:
The valueChangeListener
is the wrong tool for the job of updating the values of other input components on change of the current input. There are ways, but they are based on hacks/tricks invented during the dark JSF 1.x era when ajax didn't exist.
As you're already on JSF 2.x, just use ajax right away.
<h:selectOneMenu value="#{avocatBurController.numProf}">
<f:selectItems value="#{avocatBurController.lstAvocatbureau}" var="avcBur"
itemValue="#{avcBur.avocat.numProf}" itemLabel="#{avcBur.avocat.nom}" />
<f:ajax listener="#{avocatBurController.handleValueChange}" render="@form" />
</h:selectOneMenu>
public void handleValueChange() {
AvocatBurDao avcBurDao = new AvocatBurDao();
avcBur = avcBurDao.getAvocatBur(numProf);
avc = avcBur.getAvocat();
}
See also:
- When to use valueChangeListener or f:ajax listener?
来源:https://stackoverflow.com/questions/30993532/update-value-of-other-input-components-on-change-of-hselectonemenu