| java.lang.Object org.springframework.webflow.action.AbstractAction org.springframework.webflow.action.MultiAction org.springframework.webflow.action.FormAction
FormAction | public class FormAction extends MultiAction implements InitializingBean(Code) | | Multi-action that implements common logic dealing with input forms. This
class leverages the Spring Web data binding code to do binding and
validation.
Several action execution methods are provided:
Since this is a multi-action a subclass could add any number of additional
action execution methods, e.g. "setupReferenceData(RequestContext)", or
"processSubmit(RequestContext)".
Using this action, it becomes very easy to implement form preparation and
submission logic in your flow. One way to do this follows:
- Create a view state to display the form. In a render action of that
state, invoke
FormAction.setupForm(RequestContext) setupForm to prepare the new
form for display.
- On a matching "submit" transition execute an action that invokes
FormAction.bindAndValidate(RequestContext) bindAndValidate to bind incoming
request parameters to the form object and validate the form object.
- If there are binding or validation errors, the transition will not be
allowed and the view state will automatically be re-entered.
- If binding and validation is successful go to an action state called
"processSubmit" (or any other appropriate name). This will invoke an action
method called "processSubmit" you must provide on a subclass to process form
submission, e.g. interacting with the business logic.
- If business processing is ok, continue to a view state to display the
success view.
Here is an example implementation of such a compact form flow:
<view-state id="displayCriteria" view="searchCriteria">
<render-actions>
<action bean="formAction" method="setupForm"/>
</render-actions>
<transition on="search" to="executeSearch">
<action bean="formAction" method="bindAndValidate"/>
</transition>
</view-state>
<action-state id="executeSearch">
<action bean="formAction" method="executeSearch"/>
<transition on="success" to="displayResults"/>
</action-state>
When you need additional flexibility consider splitting the view state above
acting as a single logical form state into multiple states. For
example, you could have one action state handle form setup, a view state
trigger form display, another action state handle data binding and
validation, and another process form submission. This would be a bit more
verbose but would also give you more control over how you respond to specific
results of fine-grained actions that occur within the flow.
Subclassing hooks:
Note that this action does not provide a referenceData() hook method
similar to that of Spring MVC's SimpleFormController . If you
wish to expose reference data to populate form drop downs you can define a
custom action method in your FormAction subclass that does just that. Simply
invoke it as either a chained action as part of the setupForm state, or as a
fine grained state definition itself.
For example, you might create this method in your subclass:
public Event setupReferenceData(RequestContext context) throws Exception {
MutableAttributeMap requestScope = context.getRequestScope();
requestScope.put("refData", lookupService.getSupportingFormData());
return success();
}
... and then invoke it like this:
<view-state id="displayCriteria" view="searchCriteria">
<render-actions>
<action bean="searchFormAction" method="setupForm"/>
<action bean="searchFormAction" method="setupReferenceData"/>
</render-actions>
...
</view-state>
This style of calling multiple action methods in a chain (Chain of
Responsibility) is preferred to overridding a single action method. In
general, action method overriding is discouraged.
When it comes to validating submitted input data using a registered
org.springframework.validation.Validator , this class offers the
following options:
- If you don't want validation at all, just call
FormAction.bind(RequestContext) instead of
FormAction.bindAndValidate(RequestContext) or don't register a validator.
- If you want piecemeal validation, e.g. in a multi-page wizard, call
FormAction.bindAndValidate(RequestContext) or
FormAction.validate(RequestContext) and specify a
FormAction.VALIDATOR_METHOD_ATTRIBUTE validatorMethod action
execution attribute. This will invoke the identified custom validator method
on the validator. The validator method signature should follow the following
pattern:
public void ${validateMethodName}(${formObjectClass}, Errors)
For instance, having a action definition like this:
<action bean="searchFormAction" method="bindAndValidate">
<attribute name="validatorMethod" value="validateSearchCriteria"/>
</action>
Would result in the
public void validateSearchCriteria(SearchCriteria, Errors) method
of the registered validator being called if the form object class would be
SearchCriteria .
- If you want to do full validation using the
org.springframework.validation.Validator.validate(java.lang.Objectorg.springframework.validation.Errors) validate method of the registered validator, call
FormAction.bindAndValidate(RequestContext) or
FormAction.validate(RequestContext) without specifying a "validatorMethod" action execution attribute.
FormAction configurable properties
name |
default |
description |
formObjectName |
formObject |
The name of the form object. The form object will be set in the
configured scope using this name. |
formObjectClass |
null |
The form object class for this action. An instance of this class will
get populated and validated. Required when using a validator. |
formObjectScope |
org.springframework.webflow.execution.ScopeType.FLOW flow |
The scope in which the form object will be put. If put in flow scope the
object will be cached and reused over the life of the flow, preserving
previous values. Request scope will cause a new fresh form object instance to
be created on each request into the flow execution. |
formErrorsScope |
org.springframework.webflow.execution.ScopeType.FLASH flash |
The scope in which the form object errors instance will be put. If put
in flash scope form errors will be cached until the next user event is signaled.
|
propertyEditorRegistrar |
null |
The strategy used to register custom property editors with the data
binder. This is an alternative to overriding the
FormAction.registerPropertyEditors(PropertyEditorRegistry) hook method. |
validator |
null |
The validator for this action. The validator must support the specified
form object class. |
messageCodesResolver |
null |
Set the strategy to use for resolving errors into message codes. |
See Also: org.springframework.beans.PropertyEditorRegistrar See Also: org.springframework.validation.DataBinder See Also: ScopeType author: Erwin Vervaet author: Keith Donald |
Field Summary | |
final public static String | DEFAULT_FORM_OBJECT_NAME The default form object name ("formObject"). | final public static String | VALIDATOR_METHOD_ATTRIBUTE Optional attribute that identifies the method that should be invoked on
the configured validator instance, to support piecemeal wizard page
validation ("validatorMethod"). |
Constructor Summary | |
public | FormAction() Bean-style default constructor; creates a initially unconfigured
FormAction instance relying on default property values. | public | FormAction(Class formObjectClass) Creates a new form action that manages instance(s) of the specified form
object class. |
Method Summary | |
public Event | bind(RequestContext context) Bind incoming request parameters to allowed fields of the form object.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. | public Event | bindAndValidate(RequestContext context) Bind incoming request parameters to allowed fields of the form object and
then validate the bound form object if a validator is configured.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. | protected DataBinder | createBinder(RequestContext context, Object formObject) Create a new binder instance for the given form object and request
context. | protected Object | createFormObject(RequestContext context) Create the backing form object instance that should be managed by this
FormAction form action . | protected void | doBind(RequestContext context, DataBinder binder) Bind allowed parameters in the external context request parameter map to
the form object using given binder. | protected void | doValidate(RequestContext context, Object formObject, Errors errors) Validate given form object using a registered validator. | protected Errors | getFormErrors(RequestContext context) Convenience method that returns the form object errors for this form
action. | public ScopeType | getFormErrorsScope() Get the scope in which the Errors object will be placed. | protected Object | getFormObject(RequestContext context) Convenience method that returns the form object for this form action. | protected FormObjectAccessor | getFormObjectAccessor(RequestContext context) Factory method that returns a new form object accessor for accessing form
objects in the provided request context. | public Class | getFormObjectClass() Return the form object class for this action. | public String | getFormObjectName() Return the name of the form object in the configured scope. | public ScopeType | getFormObjectScope() Get the scope in which the form object will be placed. | public MessageCodesResolver | getMessageCodesResolver() Return the strategy to use for resolving errors into message codes. | public PropertyEditorRegistrar | getPropertyEditorRegistrar() Get the property editor registration strategy for this action's data
binders. | protected DispatchMethodInvoker | getValidateMethodInvoker() Returns a dispatcher to invoke validation methods. | public Validator | getValidator() Returns the validator for this action. | protected void | initAction() | protected void | initBinder(RequestContext context, DataBinder binder) Initialize a new binder instance. | protected void | registerPropertyEditors(RequestContext context, PropertyEditorRegistry registry) Register custom editors to perform type conversion on fields of your form
object during data binding and form display. | protected void | registerPropertyEditors(PropertyEditorRegistry registry) Register custom editors to perform type conversion on fields of your form
object during data binding and form display. | public Event | resetForm(RequestContext context) Resets the form by clearing out the form object in the specified scope
and recreating it.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. | public void | setFormErrorsScope(ScopeType errorsScope) Set the scope in which the Errors object will be placed. | public void | setFormObjectClass(Class formObjectClass) Set the form object class for this action. | public void | setFormObjectName(String formObjectName) Set the name of the form object in the configured scope. | public void | setFormObjectScope(ScopeType scopeType) Set the scope in which the form object will be placed. | public void | setMessageCodesResolver(MessageCodesResolver messageCodesResolver) Set the strategy to use for resolving errors into message codes. | public void | setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar) Set a property editor registration strategy for this action's data
binders. | public void | setValidator(Validator validator) Set the validator for this action. | public Event | setupForm(RequestContext context) Prepares a form object for display in a new form, creating it and caching
it in the
FormAction.getFormObjectScope() if necessary. | public Event | validate(RequestContext context) Validate the form object by invoking the validator if configured.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. | protected boolean | validationEnabled(RequestContext context) Return whether validation should be performed given the state of the flow
request context. |
DEFAULT_FORM_OBJECT_NAME | final public static String DEFAULT_FORM_OBJECT_NAME(Code) | | The default form object name ("formObject").
|
VALIDATOR_METHOD_ATTRIBUTE | final public static String VALIDATOR_METHOD_ATTRIBUTE(Code) | | Optional attribute that identifies the method that should be invoked on
the configured validator instance, to support piecemeal wizard page
validation ("validatorMethod").
|
FormAction | public FormAction(Class formObjectClass)(Code) | | Creates a new form action that manages instance(s) of the specified form
object class.
Parameters: formObjectClass - the class of the form object (must be instantiable) |
bind | public Event bind(RequestContext context) throws Exception(Code) | | Bind incoming request parameters to allowed fields of the form object.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. If
you need to execute custom data binding logic have your flow call this
method along with your own custom methods as part of a single action
chain. Alternatively, override the
FormAction.doBind(RequestContext,DataBinder) hook.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" "success" if there are no binding errors, "error" otherwise throws: Exception - an unrecoverable exception occured, eitherchecked or unchecked |
bindAndValidate | public Event bindAndValidate(RequestContext context) throws Exception(Code) | | Bind incoming request parameters to allowed fields of the form object and
then validate the bound form object if a validator is configured.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. If
you need to execute custom bind and validate logic have your flow call
this method along with your own custom methods as part of a single action
chain. Alternatively, override the
FormAction.doBind(RequestContext,DataBinder) or
FormAction.doValidate(RequestContext,Object,Errors) hooks.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" "success" when binding and validation is successful, "error" ifthere were binding and/or validation errors throws: Exception - an unrecoverable exception occured, eitherchecked or unchecked |
createFormObject | protected Object createFormObject(RequestContext context) throws Exception(Code) | | Create the backing form object instance that should be managed by this
FormAction form action . By default, will attempt to instantiate
a new form object instance of type
FormAction.getFormObjectClass() transiently in memory.
Subclasses should override if they need to load the form object from a
specific location or resource such as a database or filesystem.
Subclasses should override if they need to customize how a transient form
object is assembled during creation.
Parameters: context - the action execution context for accessing flow data the form object throws: IllegalStateException - if the FormAction.getFormObjectClass()property is not set and this method has not been overridden throws: Exception - when an unrecoverable exception occurs |
doBind | protected void doBind(RequestContext context, DataBinder binder) throws Exception(Code) | | Bind allowed parameters in the external context request parameter map to
the form object using given binder.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" Parameters: binder - the data binder to use throws: Exception - when an unrecoverable exception occurs |
doValidate | protected void doValidate(RequestContext context, Object formObject, Errors errors) throws Exception(Code) | | Validate given form object using a registered validator. If a
"validatorMethod" action property is specified for the currently
executing action, the identified validator method will be invoked. When
no such property is found, the defualt validate() method
is invoked.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" Parameters: formObject - the form object Parameters: errors - the errors instance to record validation errors in throws: Exception - when an unrecoverable exception occurs |
getFormErrors | protected Errors getFormErrors(RequestContext context) throws Exception(Code) | | Convenience method that returns the form object errors for this form
action. If not found in the configured scope, a new form object errors
will be created, initialized, and exposed in the confgured
FormAction.getFormErrorsScope() scope .
Keep in mind that an Errors instance wraps a form object, so a form
object will also be created if required
(see
FormAction.getFormObject(RequestContext) ).
Parameters: context - the flow request context the form errors throws: Exception - when an unrecoverable exception occurs |
getFormErrorsScope | public ScopeType getFormErrorsScope()(Code) | | Get the scope in which the Errors object will be placed.
|
getFormObjectAccessor | protected FormObjectAccessor getFormObjectAccessor(RequestContext context)(Code) | | Factory method that returns a new form object accessor for accessing form
objects in the provided request context.
Parameters: context - the flow request context the accessor |
getFormObjectClass | public Class getFormObjectClass()(Code) | | Return the form object class for this action.
|
getFormObjectName | public String getFormObjectName()(Code) | | Return the name of the form object in the configured scope.
|
getFormObjectScope | public ScopeType getFormObjectScope()(Code) | | Get the scope in which the form object will be placed.
|
getMessageCodesResolver | public MessageCodesResolver getMessageCodesResolver()(Code) | | Return the strategy to use for resolving errors into message codes.
|
getPropertyEditorRegistrar | public PropertyEditorRegistrar getPropertyEditorRegistrar()(Code) | | Get the property editor registration strategy for this action's data
binders.
|
getValidateMethodInvoker | protected DispatchMethodInvoker getValidateMethodInvoker()(Code) | | Returns a dispatcher to invoke validation methods. Subclasses could
override this to return a custom dispatcher.
|
getValidator | public Validator getValidator()(Code) | | Returns the validator for this action.
|
initAction | protected void initAction()(Code) | | |
registerPropertyEditors | protected void registerPropertyEditors(RequestContext context, PropertyEditorRegistry registry)(Code) | | Register custom editors to perform type conversion on fields of your form
object during data binding and form display. This method is called on
form errors initialization and
FormAction.initBinder(RequestContext,DataBinder) data binder initialization.
Property editors give you full control over how objects are transformed
to and from a formatted String form for display on a user interface such
as a HTML page.
This default implementation will call the
FormAction.registerPropertyEditors(PropertyEditorRegistry) simpler form of
the method not taking a RequestContext parameter.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" Parameters: registry - the property editor registry to register editors in See Also: FormAction.registerPropertyEditors(PropertyEditorRegistry) |
registerPropertyEditors | protected void registerPropertyEditors(PropertyEditorRegistry registry)(Code) | | Register custom editors to perform type conversion on fields of your form
object during data binding and form display. This method is called on
form errors initialization and
FormAction.initBinder(RequestContext,DataBinder) data binder initialization.
Property editors give you full control over how objects are transformed
to and from a formatted String form for display on a user interface such
as a HTML page.
This default implementation will simply call registerCustomEditors
on the
FormAction.getPropertyEditorRegistrar() propertyEditorRegistrar object
that has been set for the action, if any.
Parameters: registry - the property editor registry to register editors in |
resetForm | public Event resetForm(RequestContext context) throws Exception(Code) | | Resets the form by clearing out the form object in the specified scope
and recreating it.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. If
you need to execute custom reset logic have your flow call this method
along with your own custom methods as part of a single action chain.
Parameters: context - the request context "success" if the reset action completed successfully throws: Exception - if an exception occured See Also: FormAction.createFormObject(RequestContext) |
setFormErrorsScope | public void setFormErrorsScope(ScopeType errorsScope)(Code) | | Set the scope in which the Errors object will be placed. The default
if not set is
ScopeType.FLASH flash scope .
|
setFormObjectClass | public void setFormObjectClass(Class formObjectClass)(Code) | | Set the form object class for this action. An instance of this class will
get populated and validated. This is a required property if you register
a validator with the form action (
FormAction.setValidator(Validator) )!
If no form object name is set at the moment this method is called, a
form object name will be automatically generated based on the provided
form object class using
ClassUtils.getShortNameAsProperty(java.lang.Class) .
|
setFormObjectName | public void setFormObjectName(String formObjectName)(Code) | | Set the name of the form object in the configured scope. The form object
will be included in the configured scope under this name.
|
setFormObjectScope | public void setFormObjectScope(ScopeType scopeType)(Code) | | Set the scope in which the form object will be placed. The default
if not set is
ScopeType.FLOW flow scope .
|
setMessageCodesResolver | public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver)(Code) | | Set the strategy to use for resolving errors into message codes. Applies
the given strategy to all data binders used by this action.
Default is null, i.e. using the default strategy of the data binder.
See Also: FormAction.createBinder(RequestContext,Object) See Also: org.springframework.validation.DataBinder.setMessageCodesResolver(org.springframework.validation.MessageCodesResolver) |
setValidator | public void setValidator(Validator validator)(Code) | | Set the validator for this action. When setting a validator, you must also
set the
FormAction.setFormObjectClass(Class) form object class . The validator
must support the specified form object class.
|
setupForm | public Event setupForm(RequestContext context) throws Exception(Code) | | Prepares a form object for display in a new form, creating it and caching
it in the
FormAction.getFormObjectScope() if necessary. Also installs
custom property editors for formatting form object values in UI controls
such as text fields.
A new form object instance will only be created (or more generally
acquired) with a call to
FormAction.createFormObject(RequestContext) ,
if the form object does not yet exist in the configured
FormAction.getFormObjectScope() scope . If you want to reset the form
handling machinery, including creation or loading of a fresh form object
instance, call
FormAction.resetForm(RequestContext) instead.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. If
you need to execute custom form setup logic have your flow call this
method along with your own custom methods as part of a single action
chain.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" "success" when binding and validation is successful throws: Exception - an unrecoverable exception occurs, eitherchecked or unchecked See Also: FormAction.createFormObject(RequestContext) |
validate | public Event validate(RequestContext context) throws Exception(Code) | | Validate the form object by invoking the validator if configured.
NOTE: This action method is not designed to be overidden and might
become final in a future version of Spring Web Flow. If
you need to execute custom validation logic have your flow call this
method along with your own custom methods as part of a single action
chain. Alternatively, override the
FormAction.doValidate(RequestContext,Object,Errors) hook.
Parameters: context - the action execution context, for accessing and settingdata in "flow scope" or "request scope" "success" if there are no validation errors, "error" otherwise throws: Exception - an unrecoverable exception occured, eitherchecked or unchecked See Also: FormAction.getValidator() |
validationEnabled | protected boolean validationEnabled(RequestContext context)(Code) | | Return whether validation should be performed given the state of the flow
request context. Default implementation always returns true.
Parameters: context - the request context, for accessing and setting data in"flow scope" or "request scope" whether or not validation is enabled |
Fields inherited from org.springframework.webflow.action.AbstractAction | final protected Log logger(Code)(Java Doc)
|
|
|