SWF-810 - JSF ViewFactory hard codes view id to resource mapping behavior which limits flexibilty of ViewHandler viewId->resource mapping logic

This commit is contained in:
Jeremy Grelle
2008-09-18 21:04:31 +00:00
parent 4e55681d3a
commit c194e5cd3f
2 changed files with 45 additions and 35 deletions

View File

@@ -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;
}
}
}

View File

@@ -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.
*