[OmniFaces utilities] The
removeViewState()
method remove server side JSF view state associated with current request.Method
See also: Faces#getSessionAttribute()
Moreover, see also the private helpers:
See also: Reflection#findMethod()
In order to remove the current view we need to pass to Hacks#removeViewState() the current FacesContext, ResponseStateManagerand view ID. For example:
<h:commandButton value="Click me to remove view state!"
action="#{viewBean.removeViewStateAction()}"/>
import org.omnifaces.util.Faces;
import org.omnifaces.util.FacesLocal;
import org.omnifaces.util.Hacks;
...
public void removeViewStateAction() {
FacesContext facesContext = Faces.getContext();
RenderKit renderKit = FacesLocal.getRenderKit(facesContext);
ResponseStateManager responseStateManager = renderKit.getResponseStateManager();
String viewId = Faces.getViewId();
Hacks.removeViewState(facesContext, responseStateManager, viewId);
}
OmniFaces uses this method internally in RestorableViewHandlerfor removing the current view and returning the restored(new created) one:
@Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
if (isUnloadRequest(context)) {
UIViewRoot createdView = createView(context, viewId);
ResponseStateManager manager = getRenderKit(context).getResponseStateManager();
if (restoreViewRootState(context, manager, createdView)) {
context.setProcessingEvents(true);
context.getApplication().publishEvent(context, PreDestroyViewMapEvent.class, UIViewRoot.class, createdView);
Hacks.removeViewState(context, manager, viewId);
}
responseComplete();
return createdView;
}
UIViewRoot restoredView = super.restoreView(context, viewId);
if (!(isRestorableViewEnabled(context) && restoredView == null && context.isPostback())) {
return restoredView;
}
try {
UIViewRoot createdView = buildView(viewId);
return isRestorableView(createdView) ? createdView : null;
} catch (IOException e) {
throw new FacesException(e);
}
}