1 16 package org.springframework.webflow.engine.support; 17 18 import java.io.Serializable ; 19 20 import org.springframework.binding.expression.Expression; 21 import org.springframework.core.style.ToStringCreator; 22 import org.springframework.util.Assert; 23 import org.springframework.util.StringUtils; 24 import org.springframework.webflow.engine.ViewSelector; 25 import org.springframework.webflow.engine.ViewState; 26 import org.springframework.webflow.execution.RequestContext; 27 import org.springframework.webflow.execution.ViewSelection; 28 import org.springframework.webflow.execution.support.ApplicationView; 29 import org.springframework.webflow.execution.support.FlowExecutionRedirect; 30 31 50 public class ApplicationViewSelector implements ViewSelector, Serializable { 51 52 56 public static final String ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE = "alwaysRedirectOnPause"; 57 58 61 private Expression viewName; 62 63 70 private boolean redirect; 71 72 77 public ApplicationViewSelector(Expression viewName) { 78 this(viewName, false); 79 } 80 81 88 public ApplicationViewSelector(Expression viewName, boolean redirect) { 89 Assert.notNull(viewName, "The view name expression is required"); 90 this.viewName = viewName; 91 this.redirect = redirect; 92 } 93 94 97 public Expression getViewName() { 98 return viewName; 99 } 100 101 104 public boolean isRedirect() { 105 return redirect; 106 } 107 108 public boolean isEntrySelectionRenderable(RequestContext context) { 109 return !shouldRedirect(context); 110 } 111 112 public ViewSelection makeEntrySelection(RequestContext context) { 113 if (shouldRedirect(context)) { 114 return FlowExecutionRedirect.INSTANCE; 115 } 116 else { 117 return makeRefreshSelection(context); 118 } 119 } 120 121 public ViewSelection makeRefreshSelection(RequestContext context) { 122 String viewName = resolveViewName(context); 123 if (!StringUtils.hasText(viewName)) { 124 throw new IllegalStateException ("Resolved application view name was empty; programmer error! -- " 125 + "The expression that was evaluated against the request context was '" + getViewName() + "'"); 126 } 127 return createApplicationView(viewName, context); 128 } 129 130 132 137 protected String resolveViewName(RequestContext context) { 138 return (String ) getViewName().evaluate(context, null); 139 } 140 141 147 protected ApplicationView createApplicationView(String viewName, RequestContext context) { 148 return new ApplicationView(viewName, context.getModel().asMap()); 149 } 150 151 157 protected boolean shouldRedirect(RequestContext context) { 158 return context.getCurrentState() instanceof ViewState && (redirect || alwaysRedirectOnPause(context)); 159 } 160 161 167 protected boolean alwaysRedirectOnPause(RequestContext context) { 168 return context.getFlowExecutionContext().getAttributes().getBoolean( 169 ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE, Boolean.FALSE).booleanValue(); 170 } 171 172 public String toString() { 173 return new ToStringCreator(this).append("viewName", viewName).append("redirect", redirect).toString(); 174 } 175 } | Popular Tags |