Uploaded image for project: 'Spring Web Flow'
  1. Spring Web Flow
  2. SWF-238

Add support for a global "back" transition that can transition to the previous view state

    Details

      Description

      This code allows the definition of a global 'back' transition using:

      <transition on="back" to="${flowScope.previousViewStateId}"/>

      See forum http://forum.springframework.org/showthread.php?p=95731 for background information.

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

      }
      ------------------------------------------------------------------------------

      Best

      Oliver

        Attachments

          Activity

            People

            • Assignee:
              kdonald Keith Donald
              Reporter:
              ojs Oliver Siegmar
            • Votes:
              19 Vote for this issue
              Watchers:
              21 Start watching this issue

              Dates

              • Created:
                Updated: