public abstract class

AbstractWizardFormController

extends AbstractFormController
java.lang.Object
   ↳ org.springframework.context.support.ApplicationObjectSupport
     ↳ org.springframework.web.context.support.WebApplicationObjectSupport
       ↳ org.springframework.web.servlet.support.WebContentGenerator
         ↳ org.springframework.web.servlet.mvc.AbstractController
           ↳ org.springframework.web.servlet.mvc.BaseCommandController
             ↳ org.springframework.web.servlet.mvc.AbstractFormController
               ↳ org.springframework.web.servlet.mvc.AbstractWizardFormController

This class is deprecated.
as of Spring 3.0, in favor of annotated controllers

Class Overview

Form controller for typical wizard-style workflows.

In contrast to classic forms, wizards have more than one form view page. Therefore, there are various actions instead of one single submit action:

  • finish: trying to leave the wizard successfully, that is, perform its final action, and thus requiring a valid state;
  • cancel: leaving the wizard without performing its final action, and thus without regard to the validity of its current state;
  • page change: showing another wizard page, e.g. the next or previous one, with regard to "dirty back" and "dirty forward".

Finish and cancel actions can be triggered by request parameters, named PARAM_FINISH ("_finish") and PARAM_CANCEL ("_cancel"), ignoring parameter values to allow for HTML buttons. The target page for page changes can be specified by PARAM_TARGET, appending the page number to the parameter name (e.g. "_target1"). The action parameters are recognized when triggered by image buttons too (via "_finish.x", "_abort.x", or "_target1.x").

The current page number will be stored in the session. It can also be specified as request parameter PARAM_PAGE ("_page") in order to properly handle usage of the back button in a browser: In this case, a submission will always contain the correct page number, even if the user submitted from an old view.

The page can only be changed if it validates correctly, except if a "dirty back" or "dirty forward" is allowed. At finish, all pages get validated again to guarantee a consistent state.

Note that a validator's default validate method is not executed when using this class! Rather, the validatePage(Object, Errors, int) implementation should call special validateXXX methods that the validator needs to provide, validating certain pieces of the object. These can be combined to validate the elements of individual pages.

Note: Page numbering starts with 0, to be able to pass an array consisting of the corresponding view names to the "pages" bean property.

Summary

Constants
String PARAM_CANCEL Parameter triggering the cancel action.
String PARAM_FINISH Parameter triggering the finish action.
String PARAM_PAGE Parameter specifying the current page as value.
String PARAM_TARGET Parameter specifying the target page, appending the page number to the name.
[Expand]
Inherited Constants
From class org.springframework.web.servlet.mvc.BaseCommandController
From class org.springframework.web.servlet.support.WebContentGenerator
[Expand]
Inherited Fields
From class org.springframework.context.support.ApplicationObjectSupport
Public Constructors
AbstractWizardFormController()
Create a new AbstractWizardFormController.
Public Methods
final String getPageAttribute()
Return the name of the page attribute in the model.
final String[] getPages()
Return the wizard pages, i.e.
final boolean isAllowDirtyBack()
Return whether "dirty back" is allowed.
final boolean isAllowDirtyForward()
Return whether "dirty forward" is allowed.
final void setAllowDirtyBack(boolean allowDirtyBack)
Set if "dirty back" is allowed, that is, if moving to a former wizard page is allowed in case of validation errors for the current page.
final void setAllowDirtyForward(boolean allowDirtyForward)
Set if "dirty forward" is allowed, that is, if moving to a later wizard page is allowed in case of validation errors for the current page.
final void setPageAttribute(String pageAttribute)
Set the name of the page attribute in the model, containing an Integer with the current page number.
final void setPages(String[] pages)
Set the wizard pages, i.e.
Protected Methods
int getCurrentPage(HttpServletRequest request)
Return the current page number.
int getInitialPage(HttpServletRequest request, Object command)
Return the initial page of the wizard, that is, the page shown at wizard startup.
int getInitialPage(HttpServletRequest request)
Return the initial page of the wizard, that is, the page shown at wizard startup.
final int getPageCount()
Return the number of wizard pages.
int getPageCount(HttpServletRequest request, Object command)
Return the page count for this wizard form controller.
String getPageSessionAttributeName()
Return the name of the HttpSession attribute that holds the page object for this wizard form controller.
String getPageSessionAttributeName(HttpServletRequest request)
Return the name of the HttpSession attribute that holds the page object for this wizard form controller.
int getTargetPage(HttpServletRequest request, Object command, Errors errors, int currentPage)
Return the target page specified in the request.
int getTargetPage(HttpServletRequest request, int currentPage)
Return the target page specified in the request.
String getViewName(HttpServletRequest request, Object command, int page)
Return the name of the view for the specified page of this wizard form controller.
ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
Handle an invalid submit request, e.g.
boolean isCancelRequest(HttpServletRequest request)
Determine whether the incoming request is a request to cancel the processing of the current form.
boolean isFinishRequest(HttpServletRequest request)
Determine whether the incoming request is a request to finish the processing of the current form.
boolean isFormSubmission(HttpServletRequest request)
Consider an explicit finish or cancel request as a form submission too.
final void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
Calls page-specific onBindAndValidate method.
void onBindAndValidate(HttpServletRequest request, Object command, BindException errors, int page)
Callback for custom post-processing in terms of binding and validation.
void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page)
Post-process the given page after binding and validation, potentially updating its command object.
ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
Template method for processing the cancel action of this wizard.
abstract ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
Template method for processing the final action of this wizard.
final ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
Apply wizard workflow: finish, cancel, page change.
final Map referenceData(HttpServletRequest request, Object command, Errors errors)
Calls page-specific referenceData method.
Map referenceData(HttpServletRequest request, int page)
Create a reference data map for the given request, consisting of bean name/bean instance pairs as expected by ModelAndView.
Map referenceData(HttpServletRequest request, Object command, Errors errors, int page)
Create a reference data map for the given request, consisting of bean name/bean instance pairs as expected by ModelAndView.
ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors)
Show the first page as form view.
final ModelAndView showPage(HttpServletRequest request, BindException errors, int page)
Prepare the form model and view, including reference and error data, for the given page.
void validatePage(Object command, Errors errors, int page)
Template method for custom validation logic for individual pages.
void validatePage(Object command, Errors errors, int page, boolean finish)
Template method for custom validation logic for individual pages.
[Expand]
Inherited Methods
From class org.springframework.web.servlet.mvc.AbstractFormController
From class org.springframework.web.servlet.mvc.BaseCommandController
From class org.springframework.web.servlet.mvc.AbstractController
From class org.springframework.web.servlet.support.WebContentGenerator
From class org.springframework.web.context.support.WebApplicationObjectSupport
From class org.springframework.context.support.ApplicationObjectSupport
From class java.lang.Object
From interface org.springframework.context.ApplicationContextAware
From interface org.springframework.web.context.ServletContextAware
From interface org.springframework.web.servlet.mvc.Controller

Constants

public static final String PARAM_CANCEL

Parameter triggering the cancel action. Can be called from any wizard page!

Constant Value: "_cancel"

public static final String PARAM_FINISH

Parameter triggering the finish action. Can be called from any wizard page!

Constant Value: "_finish"

public static final String PARAM_PAGE

Parameter specifying the current page as value. Not necessary on form pages, but allows to properly handle usage of the back button.

Constant Value: "_page"

public static final String PARAM_TARGET

Parameter specifying the target page, appending the page number to the name.

Constant Value: "_target"

Public Constructors

public AbstractWizardFormController ()

Create a new AbstractWizardFormController.

"sessionForm" is automatically turned on, "validateOnBinding" turned off, and "cacheSeconds" set to 0 by the base class (-> no caching for all form controllers).

Public Methods

public final String getPageAttribute ()

Return the name of the page attribute in the model.

public final String[] getPages ()

Return the wizard pages, i.e. the view names for the pages. The array index corresponds to the page number.

Note that a concrete wizard form controller might override getViewName(HttpServletRequest, Object, int) to determine the view name for each page dynamically.

public final boolean isAllowDirtyBack ()

Return whether "dirty back" is allowed.

public final boolean isAllowDirtyForward ()

Return whether "dirty forward" is allowed.

public final void setAllowDirtyBack (boolean allowDirtyBack)

Set if "dirty back" is allowed, that is, if moving to a former wizard page is allowed in case of validation errors for the current page.

Parameters
allowDirtyBack if "dirty back" is allowed

public final void setAllowDirtyForward (boolean allowDirtyForward)

Set if "dirty forward" is allowed, that is, if moving to a later wizard page is allowed in case of validation errors for the current page.

Parameters
allowDirtyForward if "dirty forward" is allowed

public final void setPageAttribute (String pageAttribute)

Set the name of the page attribute in the model, containing an Integer with the current page number.

This will be necessary for single views rendering multiple view pages. It also allows for specifying the optional "_page" parameter.

Parameters
pageAttribute name of the page attribute
See Also

public final void setPages (String[] pages)

Set the wizard pages, i.e. the view names for the pages. The array index is interpreted as page number.

Parameters
pages view names for the pages

Protected Methods

protected int getCurrentPage (HttpServletRequest request)

Return the current page number. Used by processFormSubmission(HttpServletRequest, HttpServletResponse, Object, BindException).

The default implementation checks the page session attribute. Subclasses can override this for customized page determination.

Parameters
request current HTTP request
Returns
  • the current page number

protected int getInitialPage (HttpServletRequest request, Object command)

Return the initial page of the wizard, that is, the page shown at wizard startup.

The default implementation delegates to getInitialPage(HttpServletRequest).

Parameters
request current HTTP request
command the command object as returned by formBackingObject
Returns
  • the initial page number

protected int getInitialPage (HttpServletRequest request)

Return the initial page of the wizard, that is, the page shown at wizard startup.

The default implementation returns 0 for first page.

Parameters
request current HTTP request
Returns
  • the initial page number

protected final int getPageCount ()

Return the number of wizard pages. Useful to check whether the last page has been reached.

Note that a concrete wizard form controller might override getPageCount(HttpServletRequest, Object) to determine the page count dynamically. The default implementation of that extended getPageCount variant returns the static page count as determined by this getPageCount() method.

protected int getPageCount (HttpServletRequest request, Object command)

Return the page count for this wizard form controller. The default implementation delegates to getPageCount().

Can be overridden to dynamically adapt the page count.

Parameters
request current HTTP request
command the command object as returned by formBackingObject
Returns
  • the current page count
See Also

protected String getPageSessionAttributeName ()

Return the name of the HttpSession attribute that holds the page object for this wizard form controller.

Default is an internal name, of no relevance to applications, as the form session attribute is not usually accessed directly. Can be overridden to use an application-specific attribute name, which allows other code to access the session attribute directly.

Returns
  • the name of the page session attribute

protected String getPageSessionAttributeName (HttpServletRequest request)

Return the name of the HttpSession attribute that holds the page object for this wizard form controller.

The default implementation delegates to the getPageSessionAttributeName() variant without arguments.

Parameters
request current HTTP request
Returns
  • the name of the form session attribute, or null if not in session form mode

protected int getTargetPage (HttpServletRequest request, Object command, Errors errors, int currentPage)

Return the target page specified in the request.

The default implementation delegates to getTargetPage(HttpServletRequest, int). Subclasses can override this for customized target page determination.

Parameters
request current HTTP request
command form object with request parameters bound onto it
errors validation errors holder
currentPage the current page, to be returned as fallback if no target page specified
Returns
  • the page specified in the request, or current page if not found

protected int getTargetPage (HttpServletRequest request, int currentPage)

Return the target page specified in the request.

The default implementation examines "_target" parameter (e.g. "_target1"). Subclasses can override this for customized target page determination.

Parameters
request current HTTP request
currentPage the current page, to be returned as fallback if no target page specified
Returns
  • the page specified in the request, or current page if not found
See Also

protected String getViewName (HttpServletRequest request, Object command, int page)

Return the name of the view for the specified page of this wizard form controller.

The default implementation takes the view name from the getPages() array.

Can be overridden to dynamically switch the page view or to return view names for dynamically defined pages.

Parameters
request current HTTP request
command the command object as returned by formBackingObject
page the current page number
Returns
  • the current page count
See Also

protected ModelAndView handleInvalidSubmit (HttpServletRequest request, HttpServletResponse response)

Handle an invalid submit request, e.g. when in session form mode but no form object was found in the session (like in case of an invalid resubmit by the browser).

The default implementation for wizard form controllers simply shows the initial page of a new wizard form. If you want to show some "invalid submit" message, you need to override this method.

Parameters
request current HTTP request
response current HTTP response
Returns
  • a prepared view, or null if handled directly
Throws
Exception in case of errors

protected boolean isCancelRequest (HttpServletRequest request)

Determine whether the incoming request is a request to cancel the processing of the current form.

By default, this method returns true if a parameter matching the "_cancel" key is present in the request, otherwise it returns false. Subclasses may override this method to provide custom logic to detect a cancel request.

The parameter is recognized both when sent as a plain parameter ("_cancel") or when triggered by an image button ("_cancel.x").

Parameters
request current HTTP request
Returns
  • whether the request indicates to cancel form processing
See Also

protected boolean isFinishRequest (HttpServletRequest request)

Determine whether the incoming request is a request to finish the processing of the current form.

By default, this method returns true if a parameter matching the "_finish" key is present in the request, otherwise it returns false. Subclasses may override this method to provide custom logic to detect a finish request.

The parameter is recognized both when sent as a plain parameter ("_finish") or when triggered by an image button ("_finish.x").

Parameters
request current HTTP request
Returns
  • whether the request indicates to finish form processing
See Also

protected boolean isFormSubmission (HttpServletRequest request)

Consider an explicit finish or cancel request as a form submission too.

Parameters
request current HTTP request
Returns
  • if the request represents a form submission

protected final void onBindAndValidate (HttpServletRequest request, Object command, BindException errors)

Calls page-specific onBindAndValidate method.

Parameters
request current HTTP request
command the command object, still allowing for further binding
errors validation errors holder, allowing for additional custom validation
Throws
Exception

protected void onBindAndValidate (HttpServletRequest request, Object command, BindException errors, int page)

Callback for custom post-processing in terms of binding and validation. Called on each submit, after standard binding but before page-specific validation of this wizard form controller.

Note: AbstractWizardFormController does not perform standand validation on binding but rather applies page-specific validation on processing the form submission.

Parameters
request current HTTP request
command bound command
errors Errors instance for additional custom validation
page current wizard page
Throws
Exception in case of invalid state or arguments

protected void postProcessPage (HttpServletRequest request, Object command, Errors errors, int page)

Post-process the given page after binding and validation, potentially updating its command object. The passed-in request might contain special parameters sent by the page.

Only invoked when displaying another page or the same page again, not when finishing or cancelling.

Parameters
request current HTTP request
command form object with request parameters bound onto it
errors validation errors holder
page number of page to post-process
Throws
Exception in case of invalid state or arguments

protected ModelAndView processCancel (HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

Template method for processing the cancel action of this wizard.

The default implementation throws a ServletException, saying that a cancel operation is not supported by this controller. Thus, you do not need to implement this template method if you do not support a cancel operation.

Call errors.getModel() to populate the ModelAndView model with the command and the Errors instance, under the specified command name, as expected by the "spring:bind" tag.

Parameters
request current HTTP request
response current HTTP response
command form object with the current wizard state
errors Errors instance containing errors
Returns
  • the cancellation view
Throws
Exception in case of invalid state or arguments
See Also

protected abstract ModelAndView processFinish (HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

Template method for processing the final action of this wizard.

Call errors.getModel() to populate the ModelAndView model with the command and the Errors instance, under the specified command name, as expected by the "spring:bind" tag.

You can call the showPage(HttpServletRequest, BindException, int) method to return back to the wizard, in case of last-minute validation errors having been found that you would like to present to the user within the original wizard form.

Parameters
request current HTTP request
response current HTTP response
command form object with the current wizard state
errors validation errors holder
Returns
  • the finish view
Throws
Exception in case of invalid state or arguments

protected final ModelAndView processFormSubmission (HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

Apply wizard workflow: finish, cancel, page change.

Parameters
request current servlet request
response current servlet response
command form object with request parameters bound onto it
errors holder without errors (subclass can add errors if it wants to)
Returns
  • the prepared model and view, or null
Throws
Exception

protected final Map referenceData (HttpServletRequest request, Object command, Errors errors)

Calls page-specific referenceData method.

Parameters
request current HTTP request
command form object with request parameters bound onto it
errors validation errors holder
Returns
  • a Map with reference data entries, or null if none
Throws
Exception

protected Map referenceData (HttpServletRequest request, int page)

Create a reference data map for the given request, consisting of bean name/bean instance pairs as expected by ModelAndView.

The default implementation returns null. Subclasses can override this to set reference data used in the view.

Parameters
request current HTTP request
page current wizard page
Returns
  • a Map with reference data entries, or null if none
Throws
Exception in case of invalid state or arguments
See Also

protected Map referenceData (HttpServletRequest request, Object command, Errors errors, int page)

Create a reference data map for the given request, consisting of bean name/bean instance pairs as expected by ModelAndView.

The default implementation delegates to referenceData(HttpServletRequest, int). Subclasses can override this to set reference data used in the view.

Parameters
request current HTTP request
command form object with request parameters bound onto it
errors validation errors holder
page current wizard page
Returns
  • a Map with reference data entries, or null if none
Throws
Exception in case of invalid state or arguments

protected ModelAndView showForm (HttpServletRequest request, HttpServletResponse response, BindException errors)

Show the first page as form view.

This can be overridden in subclasses, e.g. to prepare wizard-specific error views in case of an Exception.

Parameters
request current HTTP request
response current HTTP response
errors validation errors holder
Returns
  • the prepared form view, or null if handled directly
Throws
Exception

protected final ModelAndView showPage (HttpServletRequest request, BindException errors, int page)

Prepare the form model and view, including reference and error data, for the given page. Can be used in processFinish(HttpServletRequest, HttpServletResponse, Object, BindException) implementations, to show the corresponding page in case of validation errors.

Parameters
request current HTTP request
errors validation errors holder
page number of page to show
Returns
  • the prepared form view
Throws
Exception in case of invalid state or arguments

protected void validatePage (Object command, Errors errors, int page)

Template method for custom validation logic for individual pages. The default implementation is empty.

Implementations will typically call fine-granular validateXXX methods of this instance's validator, combining them to validation of the corresponding pages. The validator's default validate method will not be called by a wizard form controller!

Parameters
command form object with the current wizard state
errors validation errors holder
page number of page to validate

protected void validatePage (Object command, Errors errors, int page, boolean finish)

Template method for custom validation logic for individual pages. The default implementation calls validatePage(Object, Errors, int).

Implementations will typically call fine-granular validateXXX methods of this instance's Validator, combining them to validation of the corresponding pages. The Validator's default validate method will not be called by a wizard form controller!

Parameters
command form object with the current wizard state
errors validation errors holder
page number of page to validate
finish whether this method is called during final revalidation on finish (else, it is called for validating the current page)