📄 simpleformcontroller.java
字号:
}
else {
return onSubmitRender(request, response, command, errors);
}
}
/**
* This implementation does nothing in case of errors,
* and delegates to <code>onSubmitAction</code>'s full version else.
* <p>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 <code>onSubmitAction</code>
* methods or <code>doSubmitAction</code>.
* @see #showForm
* @see #onSubmitAction(ActionRequest, ActionResponse, Object, BindException)
* @see #onSubmitAction(Object, BindException)
* @see #onSubmitAction(Object)
* @see #doSubmitAction(Object)
* @see #renderFormSubmission(RenderRequest, RenderResponse, Object, BindException)
*/
protected void processFormSubmission(
ActionRequest request, ActionResponse response, Object command, BindException errors)
throws Exception {
if (errors.hasErrors()) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
if (isRedirectAction()) {
setFormSubmit(response);
}
passRenderParameters(request, response);
}
else if (isFormChangeRequest(request)) {
logger.debug("Detected form change request -> routing request to onFormChange");
if (isRedirectAction()) {
setFormSubmit(response);
}
passRenderParameters(request, response);
onFormChange(request, response, command, errors);
}
else {
logger.debug("No errors - processing submit");
onSubmitAction(request, response, command, errors);
}
}
/**
* This implementation delegates to <code>isFormChangeRequest</code>:
* A form change request changes the appearance of the form
* and should not get validated but just show the new form.
* @see #isFormChangeRequest
*/
protected boolean suppressValidation(PortletRequest request) {
return isFormChangeRequest(request);
}
/**
* 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.
* <p>Gets called by suppressValidation and processFormSubmission.
* Consequently, this single method determines to suppress validation
* <i>and</i> to show the form view in any case.
* @param request current portlet request
* @return whether the given request is a form change request
* @see #suppressValidation
* @see #processFormSubmission
*/
protected boolean isFormChangeRequest(PortletRequest request) {
return false;
}
/**
* Called during form submission if
* {@link #isFormChangeRequest(PortletRequest)}
* returns <code>true</code>. Allows subclasses to implement custom logic
* to modify the command object to directly modify data in the form.
* <p>Default implementation delegates to
* <code>onFormChange(request, response, command)</code>.
* @param request current action request
* @param response current action response
* @param command form object with request parameters bound onto it
* @param errors validation errors holder, allowing for additional
* custom validation
* @throws Exception in case of errors
* @see #isFormChangeRequest(PortletRequest)
* @see #onFormChange(ActionRequest, ActionResponse, Object)
*/
protected void onFormChange(ActionRequest request, ActionResponse response, Object command, BindException errors)
throws Exception {
onFormChange(request, response, command);
}
/**
* Simpler <code>onFormChange</code> variant, called by the full version
* <code>onFormChange(request, response, command, errors)</code>.
* <p>Default implementation is empty.
* @param request current action request
* @param response current action response
* @param command form object with request parameters bound onto it
* @throws Exception in case of errors
* @see #onFormChange(ActionRequest, ActionResponse, Object, BindException)
*/
protected void onFormChange(ActionRequest request, ActionResponse response, Object command)
throws Exception {
}
/**
* 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.
* <p>Default implementation delegates to <code>onSubmitRender(Object, BindException)</code>.
* For simply performing a submit action and rendering the specified success view,
* do not implement an <code>onSubmitRender</code> at all.
* <p>Subclasses can override this to provide custom rendering to display results of
* the action phase. Implementations can also call <code>showForm</code> to return to the form
* if the <code>onSubmitAction</code> failed custom validation. Do <i>not</i> implement multiple
* <code>onSubmitRender</code> methods: In that case,
* just this method will be called by the controller.
* <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
* with the command and the Errors instance, under the specified command name,
* as expected by the "spring:bind" tag.
* @param request current render request
* @param response current render response
* @param command form object with request parameters bound onto it
* @param errors Errors instance without errors (subclass can add errors if it wants to)
* @return the prepared model and view
* @throws Exception in case of errors
* @see #onSubmitAction(ActionRequest, ActionResponse, Object, BindException)
* @see #onSubmitRender(Object, BindException)
* @see #doSubmitAction
* @see #showForm
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
*/
protected ModelAndView onSubmitRender(RenderRequest request, RenderResponse response, Object command, BindException errors)
throws Exception {
return onSubmitRender(command, errors);
}
/**
* 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.
* <p>Default implementation delegates to <code>onSubmitAction(Object, BindException)</code>.
* For simply performing a submit action consider implementing <code>doSubmitAction</code>
* rather than an <code>onSubmitAction</code> version.
* <p>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 <code>showForm</code> to return to the form. Do <i>not</i>
* implement multiple <code>onSubmitAction</code> methods: In that case,
* just this method will be called by the controller.
* @param request current action request
* @param response current action response
* @param command form object with request parameters bound onto it
* @param errors Errors instance without errors (subclass can add errors if it wants to)
* @throws Exception in case of errors
* @see #onSubmitRender(RenderRequest, RenderResponse, Object, BindException)
* @see #onSubmitAction(Object, BindException)
* @see #doSubmitAction
* @see org.springframework.validation.Errors
*/
protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors)
throws Exception {
onSubmitAction(command, errors);
}
/**
* Simpler <code>onSubmitRender</code> version. Called by the default implementation
* of the <code>onSubmitRender</code> version with all parameters.
* <p>Default implementation calls <code>onSubmitRender(command)</code>, 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.
* <p>Subclasses can override this to provide custom submission handling that
* does not need request and response.
* <p>Call <code>errors.getModel()</code> to populate the ModelAndView model
* with the command and the Errors instance, under the specified command name,
* as expected by the "spring:bind" tag.
* @param command form object with request parameters bound onto it
* @param errors Errors instance without errors
* @return the prepared model and view, or null
* @throws Exception in case of errors
* @see #onSubmitRender(RenderRequest, RenderResponse, Object, BindException)
* @see #onSubmitRender(Object)
* @see #onSubmitAction(Object, BindException)
* @see #setSuccessView
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
*/
protected ModelAndView onSubmitRender(Object command, BindException errors) throws Exception {
ModelAndView mv = onSubmitRender(command);
if (mv != null) {
// simplest onSubmit version implemented in custom subclass
return mv;
}
else {
// default behavior: render success view
if (getSuccessView() == null) {
throw new PortletException("successView isn't set");
}
return new ModelAndView(getSuccessView(), errors.getModel());
}
}
/**
* Simpler <code>onSubmitAction</code> version. Called by the default implementation
* of the <code>onSubmitAction</code> version with all parameters.
* <p>Default implementation calls <code>onSubmitAction(command)</code>.
* <p>Subclasses can override this to provide custom submission handling that
* does not need request and response.
* @param command form object with request parameters bound onto it
* @param errors Errors instance without errors
* @throws Exception in case of errors
* @see #onSubmitAction(ActionRequest, ActionResponse, Object, BindException)
* @see #onSubmitAction(Object)
* @see #onSubmitRender(Object, BindException)
* @see org.springframework.validation.Errors
*/
protected void onSubmitAction(Object command, BindException errors) throws Exception {
onSubmitAction(command);
}
/**
* Simplest <code>onSubmitRender</code> version. Called by the default implementation
* of the <code>onSubmitRender</code> version with command and BindException parameters.
* <p>This implementation returns null as ModelAndView, making the calling
* onSubmitRender method perform its default rendering of the success view.
* <p>Subclasses can override this to provide custom submission handling
* that just depends on the command object.
* @param command form object with request parameters bound onto it
* @return the prepared model and view, or null for default (i.e. successView)
* @throws Exception in case of errors
* @see #onSubmitRender(Object, BindException)
* @see #onSubmitAction(Object)
* @see #doSubmitAction
* @see #setSuccessView
*/
protected ModelAndView onSubmitRender(Object command) throws Exception {
return null;
}
/**
* Simplest <code>onSubmitAction</code> version. Called by the default implementation
* of the <code>onSubmitAction</code> version with command and BindException parameters.
* <p>This implementation calls <code>doSubmitAction</code>.
* <p>Subclasses can override this to provide custom submission handling
* that just depends on the command object.
* @param command form object with request parameters bound onto it
* @throws Exception in case of errors
* @see #onSubmitAction(Object, BindException)
* @see #onSubmitRender(Object)
* @see #doSubmitAction
*/
protected void onSubmitAction(Object command) throws Exception {
doSubmitAction(command);
}
/**
* Template method for submit actions. Called by the default implementation
* of the simplest onSubmitAction version.
* <p><b>This is the preferred submit callback to implement if you want to
* perform an action (like storing changes to the database) and then render
* the success view with the command and Errors instance as model.</b>
* @param command form object with request parameters bound onto it
* @throws Exception in case of errors
* @see #onSubmitAction(Object)
* @see #onSubmitRender(Object)
* @see #setSuccessView
*/
protected void doSubmitAction(Object command) throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -