import org.springframework.webflow.engine.RequestControlContext; import org.springframework.webflow.engine.support.TransitionExecutingFlowExecutionExceptionHandler; import org.springframework.webflow.execution.FlowExecutionException; import org.springframework.webflow.execution.RequestContext; public class FlowExceptionHandler extends TransitionExecutingFlowExecutionExceptionHandler { public void handle(FlowExecutionException exception, RequestControlContext context) { super.handle(exception, context); exception.printStackTrace(); } protected void exposeException(RequestContext context, FlowExecutionException exception, Throwable rootCause) { // note that all Throwables are Serializable so putting them in flash // scope should not be a problem context.getFlashScope().put(FLOW_EXECUTION_EXCEPTION_ATTRIBUTE, exception); context.getFlashScope().put(ROOT_CAUSE_EXCEPTION_ATTRIBUTE, rootCause); rootCause.printStackTrace(); } }
context.getFlashScope().put(FLOW_EXECUTION_EXCEPTION_ATTRIBUTE, exception); context.getFlashScope().put(ROOT_CAUSE_EXCEPTION_ATTRIBUTE, rootCause); Exception e = (Exception) flowRequestContext.getFlashScope().get("rootCauseException"); //e.printStackTrace(System.out); StackTraceElement[] stacktraces = e.getStackTrace(); for (int i = 0; i < 4; i++) { StackTraceElement trace = stacktraces[i]; String className = trace.getClassName(); String fieldName = trace.getFileName(); String metodName = trace.getMethodName(); int linenumber = trace.getLineNumber(); System.out.println("========= trace: " + className + " " + fieldName + " " + metodName + " " + linenumber); }
<action-state id="exceptionHandle"> <evaluate expression="testBean.handleException(flowRequestContext)" /> <transition on="exception" to="exception"/> </action-state> <exception-handler bean="flowExceptionHandler"/>
if (testState instanceof ViewState) { ViewState viewState = (ViewState) testState; try { viewState.getViewFactory().getView(context).render(); } catch (IOException e) { //Properly handle rendering errors here } }
package com.zenius.listener; import java.util.LinkedList; import org.springframework.webflow.definition.StateDefinition; import org.springframework.webflow.engine.ViewState; import org.springframework.webflow.execution.FlowExecutionListenerAdapter; import org.springframework.webflow.execution.FlowSession; import org.springframework.webflow.execution.RequestContext; public class BackToLastViewStateFlowExecutionListener extends FlowExecutionListenerAdapter { private String viewStatesName = "GLOBAL_BACK_LISTENER_VIEW_STATES"; private String backEventId = "back"; public void setViewStatesName(final String viewStatesName) { this.viewStatesName = viewStatesName; } public void setBackEventId(final String backEventId) { this.backEventId = backEventId; } @Override public void sessionStarted(final RequestContext context, final FlowSession session) { session.getScope().put(viewStatesName, new LinkedList<String>()); } private LinkedList<String> getViewStates(final RequestContext context) { @SuppressWarnings("unchecked") final LinkedList<String> viewStates = (LinkedList<String>)context.getFlowScope().get(viewStatesName); if (viewStates == null) throw new IllegalStateException("viewStates is null"); return viewStates; } @Override public void stateEntered(final RequestContext context, final StateDefinition previousState, final StateDefinition state) { // If there's no previous ViewState, there's nothing we can do... if (!(previousState instanceof ViewState)) return; // If we're entering the same state (due to a reload or // binding error), ignore that... if (previousState.getId().equals(state.getId())) return; final LinkedList<String> viewStates = getViewStates(context); final String previousStateId; if (context.getLastEvent().getId().equals(backEventId)) { viewStates.removeLast(); previousStateId = viewStates.isEmpty() ? "" : viewStates.getLast(); } else { previousStateId = previousState.getId(); viewStates.add(previousStateId); } context.getFlowScope().put("previousViewStateId", previousStateId); } }
String flowId = exception.getFlowId(); String stateId = exception.getStateId(); Event currentEvent = context.getCurrentEvent(); StateDefinition currentState = context.getCurrentState(); TransitionDefinition currentTransition = context.getCurrentTransition(); State originationgViewState = (State)context.getRequestScope().get("webflow.originatingViewState");