diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/DelegatingFlowViewResolver.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/DelegatingFlowViewResolver.java index 748abc93..ea19163c 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/DelegatingFlowViewResolver.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/DelegatingFlowViewResolver.java @@ -24,8 +24,9 @@ import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.mvc.view.FlowViewResolver; /** - * View factory implementation that delegates to the Spring-configured view resolver chain to resolve the Spring MVC - * view implementation to render. + * Delegates to a configured view resolver chain to resolve the Spring MVC view implementation to render. + * + * @see ViewResolver * * @author Keith Donald */ @@ -34,8 +35,8 @@ public class DelegatingFlowViewResolver implements FlowViewResolver { private List viewResolvers; /** - * Creates a new flow view resolver that delegates to - * @param viewResolvers + * Creates a new flow view resolver. + * @param viewResolvers the Spring MVC view resolver chain to delegate to */ public DelegatingFlowViewResolver(List viewResolvers) { this.viewResolvers = viewResolvers; @@ -56,4 +57,7 @@ public class DelegatingFlowViewResolver implements FlowViewResolver { return null; } + public String getViewIdByConvention(String viewStateId) { + return viewStateId; + } } \ No newline at end of file diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/InternalResourceFlowViewResolver.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/FlowResourceFlowViewResolver.java similarity index 73% rename from spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/InternalResourceFlowViewResolver.java rename to spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/FlowResourceFlowViewResolver.java index 760c8eb5..2aefc616 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/InternalResourceFlowViewResolver.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/FlowResourceFlowViewResolver.java @@ -26,14 +26,36 @@ import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.mvc.view.FlowViewResolver; /** - * View factory implementation that creates a Spring-MVC Internal Resource view to render a flow-relative view resource - * such as a JSP or Velocity template. + * Creates Spring-MVC Internal Resource view to render a flow-relative view resource such as a JSP template. + * + * @see JstlView + * @see InternalResourceView + * * @author Keith Donald */ -public class InternalResourceFlowViewResolver implements FlowViewResolver { +public class FlowResourceFlowViewResolver implements FlowViewResolver { private static final boolean JSTL_PRESENT = ClassUtils.isPresent("javax.servlet.jsp.jstl.fmt.LocalizationContext"); + private String defaultViewSuffix = ".jsp"; + + /** + * Returns the default view suffix when selecting views by convention. Default is .jsp. + * @return the default view suffix + */ + public String getDefaultViewSuffix() { + return defaultViewSuffix; + } + + /** + * Sets the default suffix for view templates when selecting views by convention. Default is .jsp. Respected when a + * {@link FlowResourceFlowViewResolver} is configured. + * @param defaultViewSuffix the default view suffix + */ + public void setDefaultViewSuffix(String defaultViewSuffix) { + this.defaultViewSuffix = defaultViewSuffix; + } + public View resolveView(String viewId, RequestContext context) { if (viewId.startsWith("/")) { return getViewInternal(viewId, context, context.getActiveFlow().getApplicationContext()); @@ -51,6 +73,12 @@ public class InternalResourceFlowViewResolver implements FlowViewResolver { } } + public String getViewIdByConvention(String viewStateId) { + return viewStateId + defaultViewSuffix; + } + + // internal helpers + private View getViewInternal(String viewPath, RequestContext context, ApplicationContext flowContext) { if (viewPath.endsWith(".jsp")) { if (JSTL_PRESENT) { diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcEnvironment.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcEnvironment.java index a691a707..81857071 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcEnvironment.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcEnvironment.java @@ -25,14 +25,14 @@ import org.springframework.core.enums.StaticLabeledEnum; public class MvcEnvironment extends StaticLabeledEnum { /** - * Servlet MVC. + * Spring Web Servlet MVC. */ public static final MvcEnvironment SERVLET = new MvcEnvironment(0, "servlet"); /** - * Portlet MVC. + * Spring Web Portlet MVC. */ - public static final MvcEnvironment PORTLET = new MvcEnvironment(0, "portlet"); + public static final MvcEnvironment PORTLET = new MvcEnvironment(1, "portlet"); private MvcEnvironment(int code, String label) { super(code, label); diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcViewFactoryCreator.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcViewFactoryCreator.java index d8c078c0..4de4bf06 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcViewFactoryCreator.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/builder/MvcViewFactoryCreator.java @@ -20,7 +20,12 @@ import java.util.List; import org.springframework.binding.expression.Expression; import org.springframework.binding.expression.ExpressionParser; import org.springframework.binding.format.FormatterRegistry; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.util.ClassUtils; +import org.springframework.web.portlet.context.ConfigurablePortletApplicationContext; import org.springframework.web.servlet.View; +import org.springframework.web.servlet.ViewResolver; import org.springframework.webflow.engine.builder.ViewFactoryCreator; import org.springframework.webflow.execution.ViewFactory; import org.springframework.webflow.mvc.portlet.PortletMvcViewFactory; @@ -39,13 +44,20 @@ import org.springframework.webflow.mvc.view.FlowViewResolver; * @author Keith Donald * @author Scott Andrews */ -public class MvcViewFactoryCreator implements ViewFactoryCreator { +public class MvcViewFactoryCreator implements ViewFactoryCreator, ApplicationContextAware { private MvcEnvironment environment; - private String defaultViewSuffix = ".jsp"; + private FlowViewResolver flowViewResolver = new FlowResourceFlowViewResolver(); - private FlowViewResolver flowViewResolver = new InternalResourceFlowViewResolver(); + /** + * Create a new Spring MVC View Factory Creator. + * @see #setDefaultViewSuffix(String) + * @see #setViewResolvers(List) + */ + public MvcViewFactoryCreator() { + + } /** * Returns the configured mvc environment. @@ -64,24 +76,19 @@ public class MvcViewFactoryCreator implements ViewFactoryCreator { } /** - * Returns the default view suffix when selecting views by convention. Default is .jsp. - * @return the default view suffix - */ - public String getDefaultViewSuffix() { - return defaultViewSuffix; - } - - /** - * Sets the default suffix for view names when selecting views by convention. Default is .jsp. + * Configure an {@link FlowResourceFlowViewResolver} capable of resolving view resources by applying the + * specified default resource suffix. Default is .jsp. * @param defaultViewSuffix the default view suffix */ public void setDefaultViewSuffix(String defaultViewSuffix) { - this.defaultViewSuffix = defaultViewSuffix; + FlowResourceFlowViewResolver internalResourceResolver = new FlowResourceFlowViewResolver(); + internalResourceResolver.setDefaultViewSuffix(defaultViewSuffix); + this.flowViewResolver = internalResourceResolver; } /** - * Sets the view resolvers that will be used to resolve views selected by flows. If multiple resolvers are to be - * used, the resolvers should be ordered in the manner they should be applied. + * Sets the Spring MVC {@link ViewResolver view resolvers} to delegate to resolve views selected by flows. If + * multiple resolvers are to be used, the resolvers should be ordered in the manner they should be applied. * @param viewResolvers the view resolver list */ public void setViewResolvers(List viewResolvers) { @@ -96,9 +103,18 @@ public class MvcViewFactoryCreator implements ViewFactoryCreator { this.flowViewResolver = flowViewResolver; } + public void setApplicationContext(ApplicationContext applicationContext) { + if (ClassUtils.isPresent("javax.portlet.PortletContext") + && applicationContext instanceof ConfigurablePortletApplicationContext) { + environment = MvcEnvironment.PORTLET; + } else { + environment = MvcEnvironment.SERVLET; + } + } + public ViewFactory createViewFactory(Expression viewId, ExpressionParser expressionParser, FormatterRegistry formatterRegistry) { - if (environment == null || environment == MvcEnvironment.SERVLET) { + if (environment == MvcEnvironment.SERVLET) { return new ServletMvcViewFactory(viewId, flowViewResolver, expressionParser, formatterRegistry); } else if (environment == MvcEnvironment.PORTLET) { return new PortletMvcViewFactory(viewId, flowViewResolver, expressionParser, formatterRegistry); @@ -108,11 +124,7 @@ public class MvcViewFactoryCreator implements ViewFactoryCreator { } public String getViewIdByConvention(String viewStateId) { - if (flowViewResolver instanceof DelegatingFlowViewResolver) { - return viewStateId; - } else { - return viewStateId + defaultViewSuffix; - } + return flowViewResolver.getViewIdByConvention(viewStateId); } } \ No newline at end of file diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcView.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcView.java index 12cb0309..a7f6a21e 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcView.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcView.java @@ -29,7 +29,7 @@ import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.mvc.view.AbstractMvcView; /** - * Spring Web Portlet MVC view implementation. + * The Spring Web Portlet MVC view implementation. * * @author Keith Donald * @author Scott Andrews diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcViewFactory.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcViewFactory.java index f4554dae..f7091cf9 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcViewFactory.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/portlet/PortletMvcViewFactory.java @@ -31,6 +31,13 @@ import org.springframework.webflow.mvc.view.FlowViewResolver; */ public class PortletMvcViewFactory extends AbstractMvcViewFactory { + /** + * Creates a new portlet mvc view factory. + * @param viewId the view id expression + * @param viewResolver the flow view resolver + * @param expressionParser the expression parser + * @param formatterRegistry the view formatter registry + */ public PortletMvcViewFactory(Expression viewId, FlowViewResolver viewResolver, ExpressionParser expressionParser, FormatterRegistry formatterRegistry) { super(viewId, viewResolver, expressionParser, formatterRegistry); diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/ServletMvcView.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/ServletMvcView.java index 092df049..6ce16f55 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/ServletMvcView.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/ServletMvcView.java @@ -25,7 +25,8 @@ import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.mvc.view.AbstractMvcView; /** - * Creates a new Spring Web Servlet MVC view. + * The Spring Web Servlet MVC view implementation. + * * @author Keith Donald */ public class ServletMvcView extends AbstractMvcView { diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/FlowViewResolver.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/FlowViewResolver.java index 007827fd..bcf11b81 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/FlowViewResolver.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/view/FlowViewResolver.java @@ -34,4 +34,12 @@ public interface FlowViewResolver { * @return the resolved Spring MVC view */ public View resolveView(String viewId, RequestContext context); + + /** + * Get the default id of the view to render in the provided view state by convention. + * @param viewStateId the view state id + * @return the default view id + */ + public String getViewIdByConvention(String viewStateId); + } \ No newline at end of file