Tuesday, August 25, 2009

Richfaces: immediate vs bypassUpdates attributes

If you are familiar with JSF then you know when to use immediate attribute. If not, here is a quick refresher. JSF has six different phases:
  1. Restore View - Creates or restores the previous page.

  2. Apply Request Values - Set component submitted values to request values.

  3. Process Validations - Convert and validate component values. Set component values to submitted values if valid.

  4. Update Model Values - Set backing bean values to component values.

  5. Invoke Application - Execute actionListeners and actions.

  6. Render Response - Return response.

When immediate is set to true, action components call navigation handler just after 2nd phase and skip 3-5 phases. The most common usecase is with cancel button on a form i.e. when user clicks on Cancel button, you don't want to force user to enter correct values.

Now with bypassUpdates attribute, only 4-5 phases are skipped; validation happens as usual. To credit Nick Belaevski for giving a concrete example of a sign up form, you can have one button in form to cancel (set immediate=true) and another checks whether screenname is already taken (set bypassUpdates=true). Obviously, setting both to true for same component does not make sense. If you are still not clear, here is code snippet from org. ajax4jsf.component.AjaxActionComponent
if (event instanceof ActionEvent) {
if (event.getComponent() == this) {
if (isImmediate()) {
event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
} else if (isBypassUpdates()) {
event.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
} else {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
}
}
// UICommand set Phase ID for all ActionEvents - bypass it.
getParent().queueEvent(event);
}
It should be crystal clear now...at least it was for me.

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!