From 7173a96db61bba2121f4e9c542c9783416fde3ba Mon Sep 17 00:00:00 2001 From: Jeremy Grelle Date: Thu, 16 Apr 2009 22:09:20 +0000 Subject: [PATCH] Implementing saveState method in JsfView. --- .../faces/webflow/FlowSerializedView.java | 35 ++++++++++++++++ .../faces/webflow/FlowViewStateManager.java | 42 +++++-------------- .../faces/webflow/JsfView.java | 18 +++++++- 3 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 spring-faces/src/main/java/org/springframework/faces/webflow/FlowSerializedView.java diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowSerializedView.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowSerializedView.java new file mode 100644 index 00000000..c7874652 --- /dev/null +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowSerializedView.java @@ -0,0 +1,35 @@ +package org.springframework.faces.webflow; + +import java.io.Serializable; + +import org.springframework.core.style.ToStringCreator; + +public class FlowSerializedView implements Serializable { + private Object treeStructure; + + private Object componentState; + + private String viewId; + + public FlowSerializedView(String viewId, Object treeStructure, Object componentState) { + this.viewId = viewId; + this.treeStructure = treeStructure; + this.componentState = componentState; + } + + public String getViewId() { + return this.viewId; + } + + public Object getTreeStructure() { + return this.treeStructure; + } + + public Object getComponentState() { + return this.componentState; + } + + public String toString() { + return new ToStringCreator(this).append("viewId", viewId).toString(); + } +} diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewStateManager.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewStateManager.java index 4d9e15d1..39662bbd 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewStateManager.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewStateManager.java @@ -16,7 +16,6 @@ package org.springframework.faces.webflow; import java.io.IOException; -import java.io.Serializable; import javax.faces.application.StateManager; import javax.faces.component.UIViewRoot; @@ -24,7 +23,6 @@ import javax.faces.context.FacesContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.core.style.ToStringCreator; import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.execution.RequestContextHolder; @@ -75,8 +73,8 @@ public class FlowViewStateManager extends StateManager { return; } RequestContext requestContext = RequestContextHolder.getRequestContext(); - SerializedView view = (SerializedView) requestContext.getViewScope().get(SERIALIZED_VIEW_STATE); - viewRoot.processRestoreState(context, view.componentState); + FlowSerializedView view = (FlowSerializedView) requestContext.getViewScope().get(SERIALIZED_VIEW_STATE); + viewRoot.processRestoreState(context, view.getComponentState()); logger.debug("UIViewRoot component state restored"); } @@ -85,19 +83,19 @@ public class FlowViewStateManager extends StateManager { return super.restoreTreeStructure(context, viewId, renderKitId); } RequestContext requestContext = RequestContextHolder.getRequestContext(); - SerializedView view = (SerializedView) requestContext.getViewScope().get(SERIALIZED_VIEW_STATE); - if (view == null || !view.viewId.equals(viewId)) { + FlowSerializedView view = (FlowSerializedView) requestContext.getViewScope().get(SERIALIZED_VIEW_STATE); + if (view == null || !view.getViewId().equals(viewId)) { logger.debug("No matching view in view scope"); return null; } if (logger.isDebugEnabled()) { logger.debug("Restoring view root with id '" + viewId + "' from view scope"); } - if (view.treeStructure == null) { + if (view.getTreeStructure() == null) { logger.debug("Tree structure is null indicating transient UIViewRoot; returning null"); return null; } - UIViewRoot viewRoot = new TreeStructureManager().restoreTreeStructure(view.treeStructure); + UIViewRoot viewRoot = new TreeStructureManager().restoreTreeStructure(view.getTreeStructure()); logger.debug("UIViewRoot structure restored"); return viewRoot; } @@ -127,8 +125,9 @@ public class FlowViewStateManager extends StateManager { if (!JsfUtils.isFlowRequest()) { return delegate.saveSerializedView(context); } - SerializedView view = (SerializedView) saveView(context); - return new javax.faces.application.StateManager.SerializedView(view.treeStructure, view.componentState); + FlowSerializedView view = (FlowSerializedView) saveView(context); + return new javax.faces.application.StateManager.SerializedView(view.getTreeStructure(), view + .getComponentState()); } /** @@ -145,8 +144,8 @@ public class FlowViewStateManager extends StateManager { if (logger.isDebugEnabled()) { logger.debug("Saving view root '" + context.getViewRoot().getViewId() + "' in view scope"); } - SerializedView view = new SerializedView(context.getViewRoot().getViewId(), getTreeStructureToSave(context), - getComponentStateToSave(context)); + FlowSerializedView view = new FlowSerializedView(context.getViewRoot().getViewId(), + getTreeStructureToSave(context), getComponentStateToSave(context)); requestContext.getViewScope().put(SERIALIZED_VIEW_STATE, view); return view; } @@ -161,23 +160,4 @@ public class FlowViewStateManager extends StateManager { } return viewRoot; } - - private static class SerializedView implements Serializable { - private Object treeStructure; - - private Object componentState; - - private String viewId; - - public SerializedView(String viewId, Object treeStructure, Object componentState) { - this.viewId = viewId; - this.treeStructure = treeStructure; - this.componentState = componentState; - } - - public String toString() { - return new ToStringCreator(this).append("viewId", viewId).toString(); - } - } - } diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfView.java b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfView.java index 2257e1a0..de2f8099 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfView.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfView.java @@ -72,7 +72,7 @@ public class JsfView implements View { this.viewRoot = viewRoot; } - /* + /** * Performs the standard duties of the JSF RENDER_RESPONSE phase. */ public void render() throws IOException { @@ -97,7 +97,7 @@ public class JsfView implements View { return requestContext.getRequestParameters().size() > 1; } - /* + /** * Executes postback-processing portions of the standard JSF lifecycle including APPLY_REQUEST_VALUES through * INVOKE_APPLICATION. */ @@ -114,6 +114,20 @@ public class JsfView implements View { } } + /** + * Updates the component state stored in View scope so that it remains in sync with the updated flow execution + * snapshot + */ + public void saveState() { + FacesContext facesContext = FlowFacesContext.newInstance(requestContext, facesLifecycle); + facesContext.setViewRoot(viewRoot); + try { + facesContext.getApplication().getStateManager().saveView(facesContext); + } finally { + facesContext.release(); + } + } + public Object getUserEventState() { // Set the temporary UIViewRoot state so that it will be available across the redirect return new ViewRootHolder(getViewRoot());