How to get updated model values in a valueChangeListener method?
Asked Answered
F

2

10

I am stuck with an issue on valueChangeListener. I am using JSF 1.2 on Weblogic 6.0.

I have a page, where on top of the page there are 2 radio buttons with name as Name1 and Name2. When Name1 is clicked, the Name1 details(there are about 30 fields) is displayed below the radio buttons and when Name2 is clicked, Name2 details is displayed. Now the user can updated the clicked named details. e.g. the user clicks on Name1 and changes the address field and then clicks on Name2 and changes the age of Name2. When the user clicks back Name1, the address should be shown updated with the new value and again when the user clicks on Name2, the age should be updated.

I have used valueChangeListener to tackle it because I need the old and new value of the changed event. The problem is, as the valueChangeListener is invoked at the end of the VALIDATION phase, I am not getting the updated address of the Name1 field in the valueChangeListener method. Can someone help me out to get any workaround ?

Forficate answered 5/1, 2013 at 11:52 Comment(0)
G
18

as the valueChangeListener is invoked at the end of the VALIDATION phase, I am not getting the updated address of the Name1 field in the valueChangeListener method

Queue the event to the INVOKE_ACTION phase so that it acts like as an action(listener) method.

public void valueChangeListenerMethod(ValueChangeEvent event) {
    if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }

    // Do your original job here. 
    // It will only be entered when current phase ID is INVOKE_APPLICATION.
}

As INVOKE_ACTION phase runs after UPDATE_MODEL_VALUES, the updated model values will be just available "the usual way".

Goldina answered 5/1, 2013 at 14:18 Comment(5)
Thanks a lot BalusC. It works. Whenever I am stuck at some JSF issues, I always look for your suggestions in stackoverflow and I find your answers are very simple and clear to follow. Thanks again.Forficate
Not directly connected to question, but some more info if anyone would use this solution: be aware, that if you have any other ValueChangeListeners registered for this field, they will be triggered twice as well. I guess its a quiet specific scenario, but anyway good to know.Boater
IMPORTANT note from my side: The event is put at the end of the queue, so if you have a valueChangeEvent together with an action-method the event will be executed AFTER the action-method - typically not what you want. Solution: Move it to Update-Model-Values phase, the events will be executed after updating the model (see UIViewRoot.processUpdates() ). I created a support method (see my answer) for this.Castellan
@Thies: that's not true. Most probably you was incorrectly using actionListener instead of action as "action-method". Related: https://mcmap.net/q/17396/-differences-between-action-and-actionlistenerGoldina
@BalusC: I thought of this, too - but got an exception in my program because of wrong execution order (I verified with execution under debugger control). At least when using Trinidad's <tr:commandButton> and jakarta.faces 2.3.14 the action-method gets queued as event for the IA-phase before the valueChangeListener is executed. Thus the re-queued VCE is executed after the action-method. UIViewRoot.processApplication() simply executes broadcastEvents(), there is no explicit call of the action-method afterwards (which I originally expected).Castellan
C
0

(Extension to BalusC answer)
As noted in my comment to BalusC answer, here the support method with usage example:

public static boolean moveToEndOfUpdateModel(FacesEvent event) {
    if (event.getPhaseId() == null  ||  event.getPhaseId().getOrdinal() < PhaseId.UPDATE_MODEL_VALUES.getOrdinal()) {
        // see UIViewRoot.processUpdates():  first components.processUpdates(), then broadcastEvents()
        event.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
        event.queue();
        return true;
    }
    return false;
}

(And to repeat the reason for the relevant change by me:
The event is put at the end of the queue, so if you have a valueChangeEvent together with an action-method the event will be executed after the action-method - typically not what you want.)

In the listener method I use it like this:

public void myValueChangeMethod(ValueChangeEvent vce) {
    if (JSFUtils.moveToEndOfUpdateModel(vce))  return;
    // ... perform my actions here, with access to already updated bean values ...
}

And for JSF 2+ you can better use <f:ajax listener="..."> - if you are not forced to use a component lib not supporting this (like the old Trinidad).

Castellan answered 10/3, 2021 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.