| Concrete FormController implementation that provides configurable
form and success views, and an onSubmit chain for convenient overriding.
Automatically resubmits to the form view in case of validation errors,
and renders the success view in case of a valid submission.
The workflow of this Controller does not differ much from the one described
in the
AbstractFormController AbstractFormController . The difference
is that you do not need to implement
SimpleFormController.showForm showForm ,
SimpleFormController.processFormSubmission processFormSubmission , and
SimpleFormController.renderFormSubmission renderFormSubmission : A form view and a
success view can be configured declaratively.
This controller is different from it's servlet counterpart in that it must take
into account the two phases of a portlet request: the action phase and the render
phase. See the JSR-168 spec for more details on these two phases.
Be especially aware that the action phase is called only once, but that the
render phase will be called repeatedly by the portal -- it does this every time
the page containing the portlet is updated, even if the activity is in some other
portlet. The main difference in the methods in this class is that the
onSubmit methods have all been split into onSubmitAction
and onSubmitRender to account for the two phases.
Workflow
(in addition to the superclass):
- Call to
SimpleFormController.processFormSubmission processFormSubmission which inspects
the
org.springframework.validation.Errors Errors object to see if
any errors have occurred during binding and validation.
- If errors occured, the controller will return the configured formView,
showing the form again (possibly rendering according error messages).
- If
SimpleFormController.isFormChangeRequest isFormChangeRequest is overridden and returns
true for the given request, the controller will return the formView too.
In that case, the controller will also suppress validation. Before returning the formView,
the controller will invoke
SimpleFormController.onFormChange , giving sub-classes a chance
to make modification to the command object.
This is intended for requests that change the structure of the form,
which should not cause validation and show the form in any case.
- If no errors occurred, the controller will call
SimpleFormController.onSubmitAction(ActionRequest,ActionResponse,Object,BindException) onSubmitAction during the action phase and then
SimpleFormController.onSubmitRender(RenderRequest,RenderResponse,Object,BindException) onSubmitRender during the render phase, which in case of the
default implementation delegate to
SimpleFormController.onSubmitAction(Object,BindException)onSubmitAction and
SimpleFormController.onSubmitRender(Object,BindException) onSubmitRender with just the command object.
The default implementation of the latter method will return the configured
successView . Consider just implementing
SimpleFormController.doSubmitAction doSubmitAction for simply performing a submit action during the action phase and then rendering
the success view during the render phase.
The submit behavior can be customized by overriding one of the
SimpleFormController.onSubmitAction onSubmitAction or
SimpleFormController.onSubmitRender onSubmitRender methods. Submit actions can also perform custom validation if necessary
(typically database-driven checks), calling
SimpleFormController.showForm(RenderRequest,RenderResponse,BindException) showForm in case of validation errors to show
the form view again. You do not have to override both the onSubmitAction and
onSubmitRender methods at a given level unless you truly have custom logic to
perform in both.
WARNING: Make sure that any one-time system updates (such as database
updates or file writes) are performed in either an
SimpleFormController.onSubmitAction onSubmitAction method or the
SimpleFormController.doSubmitAction doSubmitAction method. Logic in the
SimpleFormController.onSubmitRender onSubmitRender methods may be executed repeatedly by
the portal whenever the page containing the portlet is updated.
Exposed configuration properties
(and those defined by superclass):
name |
default |
description |
formView |
null |
Indicates what view to use when the user asks for a new form
or when validation errors have occurred on form submission. |
successView |
null |
Indicates what view to use when successful form submissions have
occurred. Such a success view could e.g. display a submission summary.
More sophisticated actions can be implemented by overriding one of
the
SimpleFormController.onSubmitRender(Object) onSubmitRender() methods. |
Parameters indicated with setPassRenderParameters will be
preserved if the form has errors or if a form change request occurs.
If there are render parameters you need in onSubmitRender ,
then you need to pass those forward from onSubmitAction .
Thanks to Rainer Schmitz and Nick Lothian for their suggestions!
author: John A. Lewis author: Juergen Hoeller author: Rob Harrop since: 2.0
Constructor Summary | |
public | SimpleFormController() Create a new SimpleFormController.
Subclasses should set the following properties, either in the constructor
or via a BeanFactory: commandName, commandClass, sessionForm, formView,
successView. |
Method Summary | |
protected void | doSubmitAction(Object command) Template method for submit actions. | final public String | getFormView() Return the name of the view that should be used for form display. | final public String | getSuccessView() Return the name of the view that should be shown on successful submit. | protected boolean | isFormChangeRequest(PortletRequest request) Determine whether the given request is a form change request. | protected void | onFormChange(ActionRequest request, ActionResponse response, Object command, BindException errors) Called during form submission if
SimpleFormController.isFormChangeRequest(PortletRequest) returns true . | protected void | onFormChange(ActionRequest request, ActionResponse response, Object command) Simpler onFormChange variant, called by the full version
onFormChange(request, response, command, errors) . | protected void | onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors) Submit action phase callback with all parameters. | protected void | onSubmitAction(Object command, BindException errors) Simpler onSubmitAction version. | protected void | onSubmitAction(Object command) Simplest onSubmitAction version. | protected ModelAndView | onSubmitRender(RenderRequest request, RenderResponse response, Object command, BindException errors) Submit render phase callback with all parameters. | protected ModelAndView | onSubmitRender(Object command, BindException errors) Simpler onSubmitRender version. | protected ModelAndView | onSubmitRender(Object command) Simplest onSubmitRender version. | protected void | processFormSubmission(ActionRequest request, ActionResponse response, Object command, BindException errors) This implementation does nothing in case of errors,
and delegates to onSubmitAction 's full version else.
This can only be overridden to check for an action that should be executed
without respect to binding errors, like a cancel action. | protected Map | referenceData(PortletRequest request, Object command, Errors errors) Create a reference data map for the given request and command,
consisting of bean name/bean instance pairs as expected by ModelAndView. | protected Map | referenceData(PortletRequest request) Create a reference data map for the given request. | protected ModelAndView | renderFormSubmission(RenderRequest request, RenderResponse response, Object command, BindException errors) This implementation calls showForm in case of errors,
and delegates to onSubmitRender's full version else.
This can only be overridden to check for an action that should be executed
without respect to binding errors, like a cancel action. | final public void | setFormView(String formView) Set the name of the view that should be used for form display. | final public void | setSuccessView(String successView) Set the name of the view that should be shown on successful submit. | protected ModelAndView | showForm(RenderRequest request, RenderResponse response, BindException errors) This implementation shows the configured form view, delegating to the
analogous showForm version with a controlModel argument.
Can be called within onSubmit implementations, to redirect back to the form
in case of custom validation errors (i.e. | protected ModelAndView | showForm(RenderRequest request, RenderResponse response, BindException errors, Map controlModel) This implementation shows the configured form view.
Can be called within onSubmit implementations, to redirect back to the form
in case of custom validation errors (i.e. | protected boolean | suppressValidation(PortletRequest request) This implementation delegates to isFormChangeRequest :
A form change request changes the appearance of the form
and should not get validated but just show the new form. |
getFormView | final public String getFormView()(Code) | | Return the name of the view that should be used for form display.
|
getSuccessView | final public String getSuccessView()(Code) | | Return the name of the view that should be shown on successful submit.
|
isFormChangeRequest | protected boolean isFormChangeRequest(PortletRequest request)(Code) | | Determine whether the given request is a form change request.
A form change request changes the appearance of the form
and should always show the new form, without validation.
Gets called by suppressValidation and processFormSubmission.
Consequently, this single method determines to suppress validation
and to show the form view in any case.
Parameters: request - current portlet request whether the given request is a form change request See Also: SimpleFormController.suppressValidation See Also: SimpleFormController.processFormSubmission |
onSubmitAction | protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception(Code) | | Submit action phase callback with all parameters. Called in case of submit without errors
reported by the registered validator respectively on every submit if no validator.
Default implementation delegates to onSubmitAction(Object, BindException) .
For simply performing a submit action consider implementing doSubmitAction
rather than an onSubmitAction version.
Subclasses can override this to provide custom submission handling like storing
the object to the database. Implementations can also perform custom validation and
signal the render phase to call showForm to return to the form. Do not
implement multiple onSubmitAction methods: In that case,
just this method will be called by the controller.
Parameters: request - current action request Parameters: response - current action response Parameters: command - form object with request parameters bound onto it Parameters: errors - Errors instance without errors (subclass can add errors if it wants to) throws: Exception - in case of errors See Also: SimpleFormController.onSubmitRender(RenderRequest,RenderResponse,Object,BindException) See Also: SimpleFormController.onSubmitAction(Object,BindException) See Also: SimpleFormController.doSubmitAction See Also: org.springframework.validation.Errors |
onSubmitRender | protected ModelAndView onSubmitRender(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception(Code) | | Submit render phase callback with all parameters. Called in case of submit without errors
reported by the registered validator, or on every submit if no validator.
Default implementation delegates to onSubmitRender(Object, BindException) .
For simply performing a submit action and rendering the specified success view,
do not implement an onSubmitRender at all.
Subclasses can override this to provide custom rendering to display results of
the action phase. Implementations can also call showForm to return to the form
if the onSubmitAction failed custom validation. Do not implement multiple
onSubmitRender methods: In that case,
just this method will be called by the controller.
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 render request Parameters: response - current render response Parameters: command - form object with request parameters bound onto it Parameters: errors - Errors instance without errors (subclass can add errors if it wants to) the prepared model and view throws: Exception - in case of errors See Also: SimpleFormController.onSubmitAction(ActionRequest,ActionResponse,Object,BindException) See Also: SimpleFormController.onSubmitRender(Object,BindException) See Also: SimpleFormController.doSubmitAction See Also: SimpleFormController.showForm See Also: org.springframework.validation.Errors See Also: org.springframework.validation.BindException.getModel |
onSubmitRender | protected ModelAndView onSubmitRender(Object command, BindException errors) throws Exception(Code) | | Simpler onSubmitRender version. Called by the default implementation
of the onSubmitRender version with all parameters.
Default implementation calls onSubmitRender(command) , using the
returned ModelAndView if actually implemented in a subclass. Else, the
default behavior will apply: rendering the success view with the command
and Errors instance as model.
Subclasses can override this to provide custom submission handling that
does not need request and response.
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: command - form object with request parameters bound onto it Parameters: errors - Errors instance without errors the prepared model and view, or null throws: Exception - in case of errors See Also: SimpleFormController.onSubmitRender(RenderRequest,RenderResponse,Object,BindException) See Also: SimpleFormController.onSubmitRender(Object) See Also: SimpleFormController.onSubmitAction(Object,BindException) See Also: SimpleFormController.setSuccessView See Also: org.springframework.validation.Errors See Also: org.springframework.validation.BindException.getModel |
processFormSubmission | protected void processFormSubmission(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception(Code) | | This implementation does nothing in case of errors,
and delegates to onSubmitAction 's full version else.
This can only be overridden to check for an action that should be executed
without respect to binding errors, like a cancel action. To just handle successful
submissions without binding errors, override one of the onSubmitAction
methods or doSubmitAction .
See Also: SimpleFormController.showForm See Also: SimpleFormController.onSubmitAction(ActionRequest,ActionResponse,Object,BindException) See Also: SimpleFormController.onSubmitAction(Object,BindException) See Also: SimpleFormController.onSubmitAction(Object) See Also: SimpleFormController.doSubmitAction(Object) See Also: SimpleFormController.renderFormSubmission(RenderRequest,RenderResponse,Object,BindException) |
referenceData | protected Map referenceData(PortletRequest request, Object command, Errors errors) throws Exception(Code) | | Create a reference data map for the given request and command,
consisting of bean name/bean instance pairs as expected by ModelAndView.
Default implementation delegates to referenceData(request).
Subclasses can override this to set reference data used in the view.
Parameters: request - current portlet request Parameters: command - form object with request parameters bound onto it Parameters: errors - validation errors holder a Map with reference data entries, or null if none throws: Exception - in case of invalid state or arguments See Also: ModelAndView |
referenceData | protected Map referenceData(PortletRequest request) throws Exception(Code) | | Create a reference data map for the given request.
Called by referenceData version with all parameters.
Default implementation returns null.
Subclasses can override this to set reference data used in the view.
Parameters: request - current portlet request a Map with reference data entries, or null if none throws: Exception - in case of invalid state or arguments See Also: SimpleFormController.referenceData(PortletRequest,Object,Errors) See Also: ModelAndView |
renderFormSubmission | protected ModelAndView renderFormSubmission(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception(Code) | | This implementation calls showForm in case of errors,
and delegates to onSubmitRender's full version else.
This can only be overridden to check for an action that should be executed
without respect to binding errors, like a cancel action. To just handle successful
submissions without binding errors, override one of the onSubmitRender
methods.
See Also: SimpleFormController.showForm(RenderRequest,RenderResponse,BindException) See Also: SimpleFormController.onSubmitRender(RenderRequest,RenderResponse,Object,BindException) See Also: SimpleFormController.onSubmitRender(Object,BindException) See Also: SimpleFormController.onSubmitRender(Object) See Also: SimpleFormController.processFormSubmission(ActionRequest,ActionResponse,Object,BindException) |
setFormView | final public void setFormView(String formView)(Code) | | Set the name of the view that should be used for form display.
|
setSuccessView | final public void setSuccessView(String successView)(Code) | | Set the name of the view that should be shown on successful submit.
|
showForm | protected ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors) throws Exception(Code) | | This implementation shows the configured form view, delegating to the
analogous showForm version with a controlModel argument.
Can be called within onSubmit implementations, to redirect back to the form
in case of custom validation errors (i.e. not determined by the validator).
Can be overridden in subclasses to show a custom view, writing directly
to the response or preparing the response before rendering a view.
If calling showForm with a custom control model in subclasses, it's preferable
to override the analogous showForm version with a controlModel argument
(which will handle both standard form showing and custom form showing then).
See Also: SimpleFormController.setFormView See Also: SimpleFormController.showForm(RenderRequest,RenderResponse,BindException,Map) |
showForm | protected ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors, Map controlModel) throws Exception(Code) | | This implementation shows the configured form view.
Can be called within onSubmit implementations, to redirect back to the form
in case of custom validation errors (i.e. not determined by the validator).
Can be overridden in subclasses to show a custom view, writing directly
to the response or preparing the response before rendering a view.
Parameters: request - current render request Parameters: errors - validation errors holder Parameters: controlModel - model map containing controller-specific control data(e.g. current page in wizard-style controllers or special error message) the prepared form view throws: Exception - in case of invalid state or arguments See Also: SimpleFormController.setFormView |
suppressValidation | protected boolean suppressValidation(PortletRequest request)(Code) | | This implementation delegates to isFormChangeRequest :
A form change request changes the appearance of the form
and should not get validated but just show the new form.
See Also: SimpleFormController.isFormChangeRequest |
Methods inherited from org.springframework.web.portlet.mvc.AbstractFormController | protected Object formBackingObject(PortletRequest request) throws Exception(Code)(Java Doc) final protected Object getCommand(PortletRequest request) throws Exception(Code)(Java Doc) final protected BindException getErrorsForNewForm(RenderRequest request) throws Exception(Code)(Java Doc) protected String getFormSessionAttributeName(PortletRequest request)(Code)(Java Doc) protected String getFormSessionAttributeName()(Code)(Java Doc) protected String getFormSubmitParameterName()(Code)(Java Doc) protected String getInvalidSubmitParameterName()(Code)(Java Doc) public String[] getRenderParameters()(Code)(Java Doc) protected void handleActionRequestInternal(ActionRequest request, ActionResponse response) throws Exception(Code)(Java Doc) protected void handleInvalidSubmit(ActionRequest request, ActionResponse response) throws Exception(Code)(Java Doc) protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception(Code)(Java Doc) final public boolean isBindOnNewForm()(Code)(Java Doc) protected boolean isFormSubmission(PortletRequest request)(Code)(Java Doc) protected boolean isInvalidSubmission(PortletRequest request)(Code)(Java Doc) public boolean isRedirectAction()(Code)(Java Doc) final public boolean isSessionForm()(Code)(Java Doc) protected void onBindOnNewForm(RenderRequest request, Object command, BindException errors) throws Exception(Code)(Java Doc) protected void onBindOnNewForm(RenderRequest request, Object command) throws Exception(Code)(Java Doc) protected void passRenderParameters(ActionRequest request, ActionResponse response)(Code)(Java Doc) abstract protected void processFormSubmission(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception(Code)(Java Doc) protected Map referenceData(PortletRequest request, Object command, Errors errors) throws Exception(Code)(Java Doc) abstract protected ModelAndView renderFormSubmission(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception(Code)(Java Doc) protected ModelAndView renderInvalidSubmit(RenderRequest request, RenderResponse response) throws Exception(Code)(Java Doc) final public void setBindOnNewForm(boolean bindOnNewForm)(Code)(Java Doc) final protected void setFormSubmit(ActionResponse response)(Code)(Java Doc) final protected void setInvalidSubmit(ActionResponse response)(Code)(Java Doc) public void setRedirectAction(boolean redirectAction)(Code)(Java Doc) public void setRenderParameters(String[] parameters)(Code)(Java Doc) final public void setSessionForm(boolean sessionForm)(Code)(Java Doc) abstract protected ModelAndView showForm(RenderRequest request, RenderResponse response, BindException errors) throws Exception(Code)(Java Doc) final protected ModelAndView showForm(RenderRequest request, BindException errors, String viewName) throws Exception(Code)(Java Doc) final protected ModelAndView showForm(RenderRequest request, BindException errors, String viewName, Map controlModel) throws Exception(Code)(Java Doc) final protected ModelAndView showNewForm(RenderRequest request, RenderResponse response) throws Exception(Code)(Java Doc)
|
Fields inherited from org.springframework.web.portlet.mvc.BaseCommandController | final public static String DEFAULT_COMMAND_NAME(Code)(Java Doc)
|
Methods inherited from org.springframework.web.portlet.mvc.BaseCommandController | final protected PortletRequestDataBinder bindAndValidate(PortletRequest request, Object command) throws Exception(Code)(Java Doc) final protected boolean checkCommand(Object command)(Code)(Java Doc) protected PortletRequestDataBinder createBinder(PortletRequest request, Object command) throws Exception(Code)(Java Doc) final protected Object createCommand() throws Exception(Code)(Java Doc) final public BindingErrorProcessor getBindingErrorProcessor()(Code)(Java Doc) protected Object getCommand(PortletRequest request) throws Exception(Code)(Java Doc) final public Class getCommandClass()(Code)(Java Doc) final public String getCommandName()(Code)(Java Doc) final public MessageCodesResolver getMessageCodesResolver()(Code)(Java Doc) final public PropertyEditorRegistrar[] getPropertyEditorRegistrars()(Code)(Java Doc) final protected Object getRenderCommand(RenderRequest request) throws PortletException(Code)(Java Doc) protected String getRenderCommandSessionAttributeName()(Code)(Java Doc) final protected BindException getRenderErrors(RenderRequest request) throws PortletException(Code)(Java Doc) protected String getRenderErrorsSessionAttributeName()(Code)(Java Doc) final public Validator getValidator()(Code)(Java Doc) final public Validator[] getValidators()(Code)(Java Doc) protected void initApplicationContext()(Code)(Java Doc) protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception(Code)(Java Doc) final public boolean isValidateOnBinding()(Code)(Java Doc) protected void onBind(PortletRequest request, Object command, BindException errors) throws Exception(Code)(Java Doc) protected void onBind(PortletRequest request, Object command) throws Exception(Code)(Java Doc) protected void onBindAndValidate(PortletRequest request, Object command, BindException errors) throws Exception(Code)(Java Doc) final protected void prepareBinder(PortletRequestDataBinder binder)(Code)(Java Doc) final public void setBindingErrorProcessor(BindingErrorProcessor bindingErrorProcessor)(Code)(Java Doc) final public void setCommandClass(Class commandClass)(Code)(Java Doc) final public void setCommandName(String commandName)(Code)(Java Doc) final public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver)(Code)(Java Doc) final public void setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar)(Code)(Java Doc) final public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars)(Code)(Java Doc) final protected void setRenderCommandAndErrors(ActionRequest request, Object command, BindException errors) throws Exception(Code)(Java Doc) final public void setValidateOnBinding(boolean validateOnBinding)(Code)(Java Doc) final public void setValidator(Validator validator)(Code)(Java Doc) final public void setValidators(Validator[] validators)(Code)(Java Doc) protected boolean suppressBinding(PortletRequest request)(Code)(Java Doc) protected boolean suppressValidation(PortletRequest request)(Code)(Java Doc) protected boolean useDirectFieldAccess()(Code)(Java Doc)
|
|