Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Name

ActionFlow Plugin

Publisher

Aleksandr Mashchenko

License

Open Source (ASL2)

Version

2.13.0

Compatibility

Struts 2.3.4 +

Homepage

https://github.com/aleksandr-m/struts2-actionflow

 

 

 

Wiki Markup
{rate:title=Rating|theme=dynamic}

 

 

 

Overview

A Struts2

Excerpt

plugin for creating wizards (action flows)

.

...

If you are using Maven, add this to your project POM:

Code Block
xml
xml

<dependencies>
    ...
    <dependency>
        <groupId>com.amashchenko.struts2.actionflow</groupId>
        <artifactId>struts2-actionflow-plugin</artifactId>
        <version>2.1.0</version>
    </dependency>
    ...
</dependencies>

...

Action Mappings

Code Block
xml
xml

<package name="actionflow-showcase" namespace="/" extends="actionflow-default">

    <action name="saveName" method="saveName" class="com.example.FlowAction">
        <param name="actionFlowStep">1</param>

        <result name="input">/WEB-INF/pages/name.jsp</result>
        <result name="error">/WEB-INF/pages/name.jsp</result>
        <result>/WEB-INF/pages/name-success.jsp</result>
    </action>
    <action name="savePhone" method="savePhone" class="com.example.FlowAction">
        <param name="actionFlowStep">2</param>

        <result name="input">/WEB-INF/pages/phone.jsp</result>
        <result name="error">/WEB-INF/pages/phone.jsp</result>
        <result>/WEB-INF/pages/phone-success.jsp</result>
    </action>

</package>

Form

Code Block
xml
xml

<s:form action="next">
   <s:hidden name="step" value="%{#session['actionFlowPreviousAction']}" />

   <s:textfield key="name" label="Name" />
   <s:submit value="previous" action="prev" />
   <s:submit value="next" action="next" />
</s:form>

...

Put that in your struts.xml file:

Code Block
xml
xml

<constant name="struts.mapper.action.prefix.enabled" value="true" />

Action

Code Block
java
java

@ActionFlowScope
public class FlowAction extends ActionSupport {
    @ActionFlowScope
    private String name;
}

...

Implement ActionFlowStepsAware interface in action and create getter for ActionFlowStepsData:

Code Block
java
java

public class FlowAction extends ActionSupport implements ActionFlowStepsAware {
    private ActionFlowStepsData stepsData;

    @Override
    public void setActionFlowSteps(ActionFlowStepsData stepsData) {
        this.stepsData = stepsData;
    }
    public ActionFlowStepsData getStepsData() {
        return stepsData;
    }
}

In JSP iterate over ActionFlowStepsData#steps map. Use #key and #value to get step index (starting from 1) and action name. The ActionFlowStepsData#stepIndex property holds index of current step.

Code Block
xml
xml

<ul>
    <s:iterator value="stepsData.steps">
        <s:if test="stepsData.stepIndex > key">
            <s:set var="status" value="'passed'"/>
        </s:if>
        <s:elseif test="stepsData.stepIndex == key">
            <s:set var="status" value="'active'"/>
        </s:elseif>
        <s:else>
            <s:set var="status" value="'simple'"/>
        </s:else>

        <li class="<s:property value="#status"/>">
            <s:property value="key"/> <s:property value="value"/>
        </li>
    </s:iterator>
</ul>

 

 

Controlling action flow

Available from struts2-actionflow-plugin 2.3.0

Implementing ActionFlowAware interface gives you ability to change flow of actions.

 

The nextActionFlowAction method controls which flow action will be executed next. Return the name of the flow action which should be executed after the action

which is passed as currentActionName argument. E.g. if wizard consists of three actions: 'saveName' > 'savePhone' > 'saveEmail', and

action 'savePhone' must be skipped return 'saveEmail' from this method when method argument currentActionName is 'saveName'.

On returning not a flow action name or null, action flow won't be changed (i.e. the configured next action from the flow will be executed).

 

The action properties values passed for the current action will be available in nextActionFlowAction method.

As a result of that you can skip actions based on user input and current action name.

 

Code Block
languagejava
public class FlowAction extends ActionSupport implements ActionFlowAware {

    private String name;


    @Override

    public String nextActionFlowAction(String currentActionName) {

        String action = null;



        if ("saveName".equals(currentActionName) && "skip".equals(name)) {

            action = "saveEmail";

        }


        return action;

    }
}