made FlowHandler handleException return a String; updated sample

This commit is contained in:
Keith Donald
2008-04-12 13:15:07 +00:00
parent 81258bb67a
commit 3bd270729e
8 changed files with 40 additions and 27 deletions

View File

@@ -3,9 +3,9 @@ package org.springframework.webflow.samples.booking;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.webflow.core.FlowException;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.repository.NoSuchFlowExecutionException;
import org.springframework.webflow.mvc.servlet.AbstractFlowHandler;
public class BookingFlowHandler extends AbstractFlowHandler {
@@ -15,8 +15,12 @@ public class BookingFlowHandler extends AbstractFlowHandler {
}
@Override
public ModelAndView handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView("redirect:/spring/hotels/index");
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
if (e instanceof NoSuchFlowExecutionException) {
return "hotels/index";
} else {
throw e;
}
}
}

View File

@@ -589,7 +589,12 @@ public class FlowExecutionImpl implements FlowExecution, Externalizable {
throws FlowExecutionException {
listeners.fireExceptionThrown(context, exception);
if (logger.isDebugEnabled()) {
logger.debug("Attempting to handle [" + exception + "]");
if (exception.getCause() != null) {
logger.debug("Attempting to handle [" + exception + "] with root cause [" + getRootCause(exception)
+ "]");
} else {
logger.debug("Attempting to handle [" + exception + "]");
}
}
boolean handled = false;
try {
@@ -608,6 +613,14 @@ public class FlowExecutionImpl implements FlowExecution, Externalizable {
}
}
/**
* Get the root cause of the given throwable.
*/
private Throwable getRootCause(Throwable e) {
Throwable cause = e.getCause();
return cause == null ? e : getRootCause(cause);
}
/**
* Try to handle given exception using execution exception handlers registered at the state level. Returns null if
* no handler handled the exception.

View File

@@ -3,7 +3,6 @@ package org.springframework.webflow.mvc.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.webflow.core.FlowException;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
@@ -29,7 +28,7 @@ public class AbstractFlowHandler implements FlowHandler {
return null;
}
public ModelAndView handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
return null;
}

View File

@@ -324,8 +324,9 @@ public class FlowController extends AbstractController {
if (flowId != null) {
FlowHandler handler = getFlowHandler(flowId);
if (handler != null) {
ModelAndView result = handler.handleException(e, request, response);
return result != null ? result : defaultHandleFlowException(flowId, e, request, response);
String location = handler.handleException(e, request, response);
return location != null ? createRedirectView(location, request) : defaultHandleFlowException(flowId, e,
request, response);
} else {
return defaultHandleFlowException(flowId, e, request, response);
}

View File

@@ -18,7 +18,6 @@ package org.springframework.webflow.mvc.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.webflow.core.FlowException;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
@@ -76,8 +75,8 @@ public interface FlowHandler {
* the flow executor system if no execution could be restored.
* @param request the current request
* @param response the current response
* @return the model and view to render on the occurrence of this exception, or null if the exception was not
* handled and should be handled by the caller
* @return the location of the error resource to redirect to, or null if the execution outcome was not handled and
* should be handled by the caller
*/
public ModelAndView handleException(FlowException e, HttpServletRequest request, HttpServletResponse response);
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response);
}

View File

@@ -257,8 +257,9 @@ public class FlowHandlerAdapter extends WebApplicationObjectSupport implements H
private ModelAndView handleFlowException(FlowException e, HttpServletRequest request, HttpServletResponse response,
FlowHandler handler) throws IOException {
ModelAndView result = handler.handleException(e, request, response);
return result != null ? result : defaultHandleFlowException(getFlowId(handler, request), e, request, response);
String location = handler.handleException(e, request, response);
return location != null ? createRedirectView(location, request) : defaultHandleFlowException(getFlowId(handler,
request), e, request, response);
}
public long getLastModified(HttpServletRequest request, Object handler) {

View File

@@ -294,8 +294,7 @@ public class FlowControllerTests extends TestCase {
return null;
}
public ModelAndView handleException(FlowException e, HttpServletRequest request,
HttpServletResponse response) {
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
return null;
}
});
@@ -334,8 +333,7 @@ public class FlowControllerTests extends TestCase {
return null;
}
public ModelAndView handleException(FlowException e, HttpServletRequest request,
HttpServletResponse response) {
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
return null;
}
});
@@ -375,8 +373,7 @@ public class FlowControllerTests extends TestCase {
return null;
}
public ModelAndView handleException(FlowException e, HttpServletRequest request,
HttpServletResponse response) {
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
assertEquals(flowException, e);
return null;
}

View File

@@ -61,19 +61,18 @@ public class FlowHandlerAdapterTests extends TestCase {
return "foo";
}
public ModelAndView handleException(FlowException e, HttpServletRequest request,
public String handleExecutionOutcome(String outcome, AttributeMap output, HttpServletRequest request,
HttpServletResponse response) {
if (handleException) {
return new ModelAndView("error");
if (handleExecutionOutcome) {
return "/home";
} else {
return null;
}
}
public String handleExecutionOutcome(String outcome, AttributeMap output, HttpServletRequest request,
HttpServletResponse response) {
if (handleExecutionOutcome) {
return "/home";
public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) {
if (handleException) {
return "error";
} else {
return null;
}