JSF Value Change Listener Problem
843844Oct 9 2007 — edited Oct 15 2007Hi,
I have serious problem with value change listener in JSF. I have a selectOneMenu to which I have given the valueChangeListener, every time i change the value in this selectOneMenu I want the relavent values related to it be loaded from the DB . But the problem is that i have a command button whih saves the values in the form into the database, when i click the button i'm expecting that save method sholud be called and value should be saved to the db. But the problem is that when ever i click the button first valueChangeListener's method is called and then the save button's method is called. Due to this behaviour my view is loading when it is not need and also the screen is getting distorted with unexpected results. I don't understand this behaviour in JSF. Why is that every time valueChange listener is being called even though i'm not changing any value in the selectOneMenu? Is there a way to solve this problem?
Here is the code that I wrote :
JSP PAGE :
.............
..............
<h:selectOneMenu id="selectionid"
value="#{tagGeneratorTagView.imageType}" valueChangeListener="#{imageHandler.changeImageType}"
onchange="submit();">
.............
..............
<h:commandButton value="#{msgHelper.save_settings}"
action="#{tagGeneratorHandler.saveSettings}" />
..........
..........
Java Code (Handler):
public class ImageHandler extends AbstractHandler {
public String changeImageType(ValueChangeEvent ve) {
String deployType = (String) ve.getNewValue();
if (deployType.equals("Button") ) {
showDisplayOption.setRendered(Boolean.TRUE);
showText.setRendered(Boolean.FALSE);
}
}
public String saveSettings() {
// code to save to DB
}
}
I have added the following code to the valueChangeListener Method, but when this has only aggravated the problem by calling the valueChangeListener Method twice :-(. Once before save method is called and once after save method is called.
public class ImageHandler extends AbstractHandler {
............
............
...........
if (PhaseId.ANY_PHASE.equals(ve.getPhaseId())) {
ValueChangeEvent valueChangeEvent = new ValueChangeEvent(ve.getComponent(), ve.getOldValue(), ve.getNewValue());
valueChangeEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
getContext().getViewRoot().queueEvent(valueChangeEvent);
return SUCCESS;
}
}
How can i solve this problem? I want to have control over what method is being called.
Jsf is great but i find valueChangeListener always troubles.
Any help will be greatly appreciated.
Thanks in advance,
Kranthi.