simpleformcontroller.java
来自「一个关于Spring框架的示例应用程序,简单使用,可以参考.」· Java 代码 · 共 385 行 · 第 1/2 页
JAVA
385 行
* <p>Default implementation delegates to referenceData(request).
* Subclasses can override this to set reference data used in the view.
* @param request current HTTP request
* @param command form object with request parameters bound onto it
* @param errors validation errors holder
* @return a Map with reference data entries, or null if none
* @throws Exception in case of invalid state or arguments
* @see ModelAndView
*/
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
return referenceData(request);
}
/**
* Create a reference data map for the given request.
* Called by referenceData version with all parameters.
* <p>Default implementation returns null.
* Subclasses can override this to set reference data used in the view.
* @param request current HTTP request
* @return a Map with reference data entries, or null if none
* @throws Exception in case of invalid state or arguments
* @see #referenceData(HttpServletRequest, Object, Errors)
* @see ModelAndView
*/
protected Map referenceData(HttpServletRequest request) throws Exception {
return null;
}
/**
* This implementation calls <code>showForm</code> in case of errors,
* and delegates to <code>onSubmit</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>onSubmit</code>
* methods or <code>doSubmitAction</code>.
* @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() || isFormChangeRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
}
else {
logger.debug("No errors -> processing submit");
return onSubmit(request, response, command, errors);
}
}
/**
* 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 HTTP request
* @return whether the given request is a form change request
* @see #suppressValidation
* @see #processFormSubmission
*/
protected boolean isFormChangeRequest(HttpServletRequest request) {
return false;
}
/**
* 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(HttpServletRequest request) {
return isFormChangeRequest(request);
}
/**
* 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>Default implementation delegates to onSubmit(Object, BindException).
* For simply performing a submit action and rendering the specified success view,
* consider implementing doSubmitAction rather than an onSubmit 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
* 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 null
* @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 onSubmit version. Called by the default implementation of the onSubmit
* version with all parameters.
* <p>Default implementation calls onSubmit(command), using the returned ModelAndView
* if actually implemented in a subclass. Else, the default behavior is applied:
* 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 #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 version 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 onSubmit version. Called by the default implementation of the
* onSubmit version with command and BindException parameters.
* <p>This implementation calls <code>doSubmitAction</code> and returns null
* as ModelAndView, making the calling onSubmit 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
* <code>onSubmit(command, errors)</code> or <code>doSubmitAction(command)</code>,
* 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 null for default (i.e. 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 onSubmit 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>
* 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 + =
减小字号Ctrl + -
显示快捷键?