This commit is contained in:
Keith Donald
2008-04-25 03:41:46 +00:00
parent e31f5e9ef8
commit 26abaa350a
8 changed files with 93 additions and 33 deletions

View File

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

View File

@@ -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) {

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 {

View File

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