From c194e5cd3f798f5305460f0c0fcdafd815bd013a Mon Sep 17 00:00:00 2001 From: Jeremy Grelle Date: Thu, 18 Sep 2008 21:04:31 +0000 Subject: [PATCH] SWF-810 - JSF ViewFactory hard codes view id to resource mapping behavior which limits flexibilty of ViewHandler viewId->resource mapping logic --- .../faces/webflow/FlowViewHandler.java | 52 ++++++++++++++++--- .../faces/webflow/JsfViewFactory.java | 28 +--------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewHandler.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewHandler.java index b5879869..5beb93e4 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewHandler.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowViewHandler.java @@ -23,6 +23,10 @@ import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; +import org.springframework.context.ApplicationContext; +import org.springframework.core.io.ContextResource; +import org.springframework.core.io.Resource; +import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.execution.RequestContextHolder; /** @@ -55,16 +59,28 @@ public class FlowViewHandler extends ViewHandler { } } + public UIViewRoot createView(FacesContext context, String viewId) { + String resourcePath = viewId; + if (JsfUtils.isFlowRequest()) { + resourcePath = resolveResourcePath(RequestContextHolder.getRequestContext(), viewId); + } + return delegate.createView(context, resourcePath); + } + + public UIViewRoot restoreView(FacesContext context, String viewId) { + String resourcePath = viewId; + if (JsfUtils.isFlowRequest()) { + resourcePath = resolveResourcePath(RequestContextHolder.getRequestContext(), viewId); + } + return delegate.restoreView(context, resourcePath); + } + // ------------------- Pass-through delegate methods ------------------// public String calculateRenderKitId(FacesContext context) { return delegate.calculateRenderKitId(context); } - public UIViewRoot createView(FacesContext context, String viewId) { - return delegate.createView(context, viewId); - } - public String getResourceURL(FacesContext context, String path) { return delegate.getResourceURL(context, path); } @@ -73,12 +89,32 @@ public class FlowViewHandler extends ViewHandler { delegate.renderView(context, viewToRender); } - public UIViewRoot restoreView(FacesContext context, String viewId) { - return delegate.restoreView(context, viewId); - } - public void writeState(FacesContext context) throws IOException { delegate.writeState(context); } + // --------------------- Private Helpers ------------------------------// + + private String resolveResourcePath(RequestContext context, String viewId) { + if (viewId.startsWith("/")) { + return viewId; + } else { + ApplicationContext flowContext = context.getActiveFlow().getApplicationContext(); + if (flowContext == null) { + throw new IllegalStateException("A Flow ApplicationContext is required to resolve Flow View Resources"); + } + Resource viewResource = flowContext.getResource(viewId); + if (!(viewResource instanceof ContextResource)) { + throw new IllegalStateException( + "A ContextResource is required to get relative view paths within this context; the resource was " + + viewResource); + } + String viewPath = ((ContextResource) viewResource).getPathWithinContext(); + if (!viewPath.startsWith("/")) { + viewPath = "/" + viewPath; + } + return viewPath; + } + } + } diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfViewFactory.java b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfViewFactory.java index e052ddaa..92e71617 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfViewFactory.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfViewFactory.java @@ -32,9 +32,6 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.binding.expression.Expression; -import org.springframework.context.ApplicationContext; -import org.springframework.core.io.ContextResource; -import org.springframework.core.io.Resource; import org.springframework.faces.ui.AjaxViewRoot; import org.springframework.js.ajax.SpringJavascriptAjaxHandler; import org.springframework.webflow.context.ExternalContext; @@ -82,7 +79,7 @@ public class JsfViewFactory implements ViewFactory { viewHandler.initView(facesContext); } JsfView view; - String viewName = resolveViewName(context); + String viewName = (String) viewIdExpression.getValue(context); if (viewAlreadySet(facesContext)) { if (logger.isDebugEnabled()) { logger.debug("Existing view root found with id '" + facesContext.getViewRoot().getId() + "'"); @@ -159,29 +156,6 @@ public class JsfViewFactory implements ViewFactory { } } - private String resolveViewName(RequestContext context) { - String viewId = (String) viewIdExpression.getValue(context); - if (viewId.startsWith("/")) { - return viewId; - } else { - ApplicationContext flowContext = context.getActiveFlow().getApplicationContext(); - if (flowContext == null) { - throw new IllegalStateException("A Flow ApplicationContext is required to resolve Flow View Resources"); - } - Resource viewResource = flowContext.getResource(viewId); - if (!(viewResource instanceof ContextResource)) { - throw new IllegalStateException( - "A ContextResource is required to get relative view paths within this context; the resource was " - + viewResource); - } - String viewPath = ((ContextResource) viewResource).getPathWithinContext(); - if (!viewPath.startsWith("/")) { - viewPath = "/" + viewPath; - } - return viewPath; - } - } - /** * Walk the component tree to perform any required per-component operations. *