DEVFYI - Developer Resource - FYI

Struts Questions and Answers

Part:   1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17   18  19  20  21  22  23  24  25 

62. How can I create a wizard workflow?

The basic idea is a series of actions with next, back, cancel and finish actions with a common bean. Using a LookupDispatchAction is reccomended as it fits the design pattern well and can be internationalized easily. Since the bean is shared, each choice made will add data to the wizards base of information. A sample of struts-config.xml follows:


            < form-beans>
                <form-bean  name="MyWizard"
                            type="forms.MyWizard" />
            </form-beans>

        <!-- the first screen of the wizard (next action only available) -->
        <!-- no validation, since the finish action is not available -->
            <actions>
                <action path="/mywizard1"
                        type="actions.MyWizard"
                        name="MyWizard"
                        validate="false"
                        input="/WEB-INF/jsp/mywizard1.jsp">
                    <forward name="next"
                        path="/WEB-INF/jsp/mywizard2.jsp" />
                    <forward name="cancel"
                        path="/WEB-INF/jsp/mywizardcancel.jsp" />
                </action>

                <!-- the second screen of the wizard (back, next and finish) -->
                <!-- since finish action is available, bean should validated, note
        validation should not necessarily validate if back action requested, you
        might delay validation or do conditional validation -->
                <action path="/mywizard2"
                        type="actions.MyWizard"
                        name="MyWizard"
                        validate="true"
                        input="/WEB-INF/jsp/mywizard2.jsp">
                    <forward name="back"
                        path="/WEB-INF/jsp/mywizard1.jsp" />
                    <forward name="next"
                        path="/WEB-INF/jsp/mywizard3.jsp" />
                    <forward name="finish"
                        path="/WEB-INF/jsp/mywizarddone.jsp" />
                    <forward name="cancel"
                        path="/WEB-INF/jsp/mywizardcancel.jsp" />
                </action>

                <!-- the last screen of the wizard (back, finish and cancel only) -->
                <action path="/mywizard3"
                        type="actions.MyWizard"
                        name="MyWizard"
                        validate="true"
                        input="/WEB-INF/jsp/mywizard3.jsp">
                    <forward name="back"
                        path="/WEB-INF/jsp/mywizard2.jsp" />
                    <forward name="finish"
                        path="/WEB-INF/jsp/mywizarddone.jsp" />
                    <forward name="cancel"
                        path="/WEB-INF/jsp/mywizardcancel.jsp" />
                </action>
 

The pieces of the wizard are as follows:
forms.MyWizard.java - the form bean holding the information required
actions.MyWizard.java - the actions of the wizard, note the use of LookupDispatchAction allows for one action class with several methods. All the real work will be done in the 'finish' method.
mywizard[x].jsp - the data collection jsp's
mywizarddone.jsp - the 'success' page
mywizardcancel.jsp - the 'cancel' page

Part:   1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17   18  19  20  21  22  23  24  25 

Struts Questions and Answers