📄 simpleformcontroller.java
字号:
/**
* This implementation calls
* {@link #showForm(HttpServletRequest, HttpServletResponse, BindException)}
* in case of errors, and delegates to the full
* {@link #onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)}'s
* variant 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>onSubmit</code>
* methods or {@link #doSubmitAction}.
* @see #showForm(HttpServletRequest, HttpServletResponse, BindException)
* @see #onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)
* @see #onSubmit(Object, BindException)
* @see #onSubmit(Object)
* @see #doSubmitAction(Object)
*/
protected ModelAndView processFormSubmission(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
if (errors.hasErrors()) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
}
else if (isFormChangeRequest(request, command)) {
logger.debug("Detected form change request -> routing request to onFormChange");
onFormChange(request, response, command, errors);
return showForm(request, response, errors);
}
else {
logger.debug("No errors -> processing submit");
return onSubmit(request, response, command, errors);
}
}
/**
* This implementation delegates to {@link #isFormChangeRequest(HttpServletRequest, Object)}:
* 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(HttpServletRequest request, Object command) {
return isFormChangeRequest(request, command);
}
/**
* 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 {@link #suppressValidation} and {@link #processFormSubmission}.
* Consequently, this single method determines to suppress validation
* <i>and</i> to show the form view in any case.
* <p>The default implementation delegates to
* {@link #isFormChangeRequest(javax.servlet.http.HttpServletRequest)}.
* @param request current HTTP request
* @param command form object with request parameters bound onto it
* @return whether the given request is a form change request
* @see #suppressValidation
* @see #processFormSubmission
*/
protected boolean isFormChangeRequest(HttpServletRequest request, Object command) {
return isFormChangeRequest(request);
}
/**
* Simpler <code>isFormChangeRequest</code> variant, called by the full
* variant {@link #isFormChangeRequest(HttpServletRequest, Object)}.
* <p>The default implementation is empty.
* @param request current HTTP request
* @return whether the given request is a form change request
* @see #suppressValidation
* @see #processFormSubmission
*/
protected boolean isFormChangeRequest(HttpServletRequest request) {
return false;
}
/**
* Called during form submission if
* {@link #isFormChangeRequest(javax.servlet.http.HttpServletRequest)}
* returns <code>true</code>. Allows subclasses to implement custom logic
* to modify the command object to directly modify data in the form.
* <p>The default implementation delegates to
* {@link #onFormChange(HttpServletRequest, HttpServletResponse, Object, BindException)}.
* @param request current servlet request
* @param response current servlet 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(HttpServletRequest)
* @see #onFormChange(HttpServletRequest, HttpServletResponse, Object)
*/
protected void onFormChange(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
onFormChange(request, response, command);
}
/**
* Simpler <code>onFormChange</code> variant, called by the full variant
* {@link #onFormChange(HttpServletRequest, HttpServletResponse, Object, BindException)}.
* <p>The default implementation is empty.
* @param request current servlet request
* @param response current servlet response
* @param command form object with request parameters bound onto it
* @throws Exception in case of errors
* @see #onFormChange(HttpServletRequest, HttpServletResponse, Object, BindException)
*/
protected void onFormChange(HttpServletRequest request, HttpServletResponse response, Object command)
throws Exception {
}
/**
* Submit callback with all parameters. Called in case of submit without errors
* reported by the registered validator, or on every submit if no validator.
* <p>The default implementation delegates to {@link #onSubmit(Object, BindException)}.
* For simply performing a submit action and rendering the specified success
* view, consider implementing {@link #doSubmitAction} rather than an
* <code>onSubmit</code> variant.
* <p>Subclasses can override this to provide custom submission handling like storing
* the object to the database. Implementations can also perform custom validation and
* call showForm to return to the form. Do <i>not</i> implement multiple onSubmit
* 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 servlet request
* @param response current servlet 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, or <code>null</code>
* @throws Exception in case of errors
* @see #onSubmit(Object, BindException)
* @see #doSubmitAction
* @see #showForm
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
*/
protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
return onSubmit(command, errors);
}
/**
* Simpler <code>onSubmit</code> variant.
* Called by the default implementation of the
* {@link #onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)}
* variant with all parameters.
* <p>The default implementation calls {@link #onSubmit(Object)}, 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
* @throws Exception in case of errors
* @see #onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)
* @see #onSubmit(Object)
* @see #setSuccessView
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
*/
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
ModelAndView mv = onSubmit(command);
if (mv != null) {
// simplest onSubmit variant implemented in custom subclass
return mv;
}
else {
// default behavior: render success view
if (getSuccessView() == null) {
throw new ServletException("successView isn't set");
}
return new ModelAndView(getSuccessView(), errors.getModel());
}
}
/**
* Simplest <code>onSubmit</code> variant. Called by the default implementation
* of the {@link #onSubmit(Object, BindException)} variant.
* <p>This implementation calls {@link #doSubmitAction(Object)} and returns
* <code>null</code> as ModelAndView, making the calling <code>onSubmit</code>
* 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. It's preferable to use either
* {@link #onSubmit(Object, BindException)} or {@link #doSubmitAction(Object)},
* though: Use the former when you want to build your own ModelAndView; use the
* latter when you want to perform an action and forward to the successView.
* @param command form object with request parameters bound onto it
* @return the prepared model and view, or <code>null</code> for default
* (that is, rendering the configured "successView")
* @throws Exception in case of errors
* @see #onSubmit(Object, BindException)
* @see #doSubmitAction
* @see #setSuccessView
*/
protected ModelAndView onSubmit(Object command) throws Exception {
doSubmitAction(command);
return null;
}
/**
* Template method for submit actions. Called by the default implementation
* of the simplest {@link #onSubmit(Object)} variant.
* <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>
* You don't need to care about the success ModelAndView here.
* @param command form object with request parameters bound onto it
* @throws Exception in case of errors
* @see #onSubmit(Object)
* @see #setSuccessView
*/
protected void doSubmitAction(Object command) throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -