📄 abstractwizardformcontroller.java
字号:
* @param request current HTTP request
* @param command the command object as returned by formBackingObject
* @return the current page count
* @see #getPageCount
*/
protected String getViewName(HttpServletRequest request, Object command, int page) {
return getPages()[page];
}
/**
* Return the initial page of the wizard, that is, the page shown at wizard startup.
* <p>The default implementation delegates to {@link #getInitialPage(HttpServletRequest)}.
* @param request current HTTP request
* @param command the command object as returned by formBackingObject
* @return the initial page number
* @see #getInitialPage(HttpServletRequest)
* @see #formBackingObject
*/
protected int getInitialPage(HttpServletRequest request, Object command) {
return getInitialPage(request);
}
/**
* Return the initial page of the wizard, that is, the page shown at wizard startup.
* <p>The default implementation returns 0 for first page.
* @param request current HTTP request
* @return the initial page number
*/
protected int getInitialPage(HttpServletRequest request) {
return 0;
}
/**
* Return the name of the HttpSession attribute that holds the page object
* for this wizard form controller.
* <p>The default implementation delegates to the {@link #getPageSessionAttributeName()}
* variant without arguments.
* @param request current HTTP request
* @return the name of the form session attribute, or <code>null</code> if not in session form mode
* @see #getPageSessionAttributeName
* @see #getFormSessionAttributeName(javax.servlet.http.HttpServletRequest)
* @see javax.servlet.http.HttpSession#getAttribute
*/
protected String getPageSessionAttributeName(HttpServletRequest request) {
return getPageSessionAttributeName();
}
/**
* Return the name of the HttpSession attribute that holds the page object
* for this wizard form controller.
* <p>Default is an internal name, of no relevance to applications, as the form
* session attribute is not usually accessed directly. Can be overridden to use
* an application-specific attribute name, which allows other code to access
* the session attribute directly.
* @return the name of the page session attribute
* @see #getFormSessionAttributeName
* @see javax.servlet.http.HttpSession#getAttribute
*/
protected String getPageSessionAttributeName() {
return getClass().getName() + ".PAGE." + getCommandName();
}
/**
* Handle an invalid submit request, e.g. when in session form mode but no form object
* was found in the session (like in case of an invalid resubmit by the browser).
* <p>The default implementation for wizard form controllers simply shows the initial page
* of a new wizard form. If you want to show some "invalid submit" message, you need
* to override this method.
* @param request current HTTP request
* @param response current HTTP response
* @return a prepared view, or <code>null</code> if handled directly
* @throws Exception in case of errors
* @see #showNewForm
* @see #setBindOnNewForm
*/
protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
throws Exception {
return showNewForm(request, response);
}
/**
* Apply wizard workflow: finish, cancel, page change.
*/
protected final ModelAndView processFormSubmission(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
int currentPage = getCurrentPage(request);
// Remove page session attribute, provide copy as request attribute.
String pageAttrName = getPageSessionAttributeName(request);
if (isSessionForm()) {
if (logger.isDebugEnabled()) {
logger.debug("Removing page session attribute [" + pageAttrName + "]");
}
request.getSession().removeAttribute(pageAttrName);
}
request.setAttribute(pageAttrName, new Integer(currentPage));
// cancel?
if (isCancelRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Cancelling wizard for form bean '" + getCommandName() + "'");
}
return processCancel(request, response, command, errors);
}
// finish?
if (isFinishRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Finishing wizard for form bean '" + getCommandName() + "'");
}
return validatePagesAndFinish(request, response, command, errors, currentPage);
}
// Normal submit: validate current page and show specified target page.
if (!suppressValidation(request, command, errors)) {
if (logger.isDebugEnabled()) {
logger.debug("Validating wizard page " + currentPage + " for form bean '" + getCommandName() + "'");
}
validatePage(command, errors, currentPage, false);
}
// Give subclasses a change to perform custom post-procession
// of the current page and its command object.
postProcessPage(request, command, errors, currentPage);
int targetPage = getTargetPage(request, command, errors, currentPage);
if (logger.isDebugEnabled()) {
logger.debug("Target page " + targetPage + " requested");
}
if (targetPage != currentPage) {
if (!errors.hasErrors() || (this.allowDirtyBack && targetPage < currentPage) ||
(this.allowDirtyForward && targetPage > currentPage)) {
// Allowed to go to target page.
return showPage(request, errors, targetPage);
}
}
// Show current page again.
return showPage(request, errors, currentPage);
}
/**
* Return the current page number. Used by processFormSubmission.
* <p>The default implementation checks the page session attribute.
* Subclasses can override this for customized page determination.
* @see #processFormSubmission
* @see #getPageSessionAttributeName
*/
protected int getCurrentPage(HttpServletRequest request) {
// Check for overriding attribute in request.
String pageAttrName = getPageSessionAttributeName(request);
Integer pageAttr = (Integer) request.getAttribute(pageAttrName);
if (pageAttr != null) {
return pageAttr.intValue();
}
// Check for explicit request parameter.
String pageParam = request.getParameter(PARAM_PAGE);
if (pageParam != null) {
return Integer.parseInt(pageParam);
}
// Check for original attribute in session.
if (isSessionForm()) {
pageAttr = (Integer) request.getSession().getAttribute(pageAttrName);
if (pageAttr != null) {
return pageAttr.intValue();
}
}
throw new IllegalStateException(
"Page attribute [" + pageAttrName + "] neither found in session nor in request");
}
/**
* Determine whether the incoming request is a request to finish the
* processing of the current form.
* <p>By default, this method returns <code>true</code> if a parameter
* matching the "_finish" key is present in the request, otherwise it
* returns <code>false</code>. Subclasses may override this method
* to provide custom logic to detect a finish request.
* <p>The parameter is recognized both when sent as a plain parameter
* ("_finish") or when triggered by an image button ("_finish.x").
* @param request current HTTP request
* @see #PARAM_FINISH
*/
protected boolean isFinishRequest(HttpServletRequest request) {
return WebUtils.hasSubmitParameter(request, PARAM_FINISH);
}
/**
* Determine whether the incoming request is a request to cancel the
* processing of the current form.
* <p>By default, this method returns <code>true</code> if a parameter
* matching the "_cancel" key is present in the request, otherwise it
* returns <code>false</code>. Subclasses may override this method
* to provide custom logic to detect a cancel request.
* <p>The parameter is recognized both when sent as a plain parameter
* ("_cancel") or when triggered by an image button ("_cancel.x").
* @param request current HTTP request
* @see #PARAM_CANCEL
*/
protected boolean isCancelRequest(HttpServletRequest request) {
return WebUtils.hasSubmitParameter(request, PARAM_CANCEL);
}
/**
* Return the target page specified in the request.
* <p>The default implementation delegates to {@link #getTargetPage(HttpServletRequest, int)}.
* Subclasses can override this for customized target page determination.
* @param request current HTTP request
* @param command form object with request parameters bound onto it
* @param errors validation errors holder
* @param currentPage the current page, to be returned as fallback
* if no target page specified
* @return the page specified in the request, or current page if not found
* @see #getTargetPage(HttpServletRequest, int)
*/
protected int getTargetPage(HttpServletRequest request, Object command, Errors errors, int currentPage) {
return getTargetPage(request, currentPage);
}
/**
* Return the target page specified in the request.
* <p>The default implementation examines "_target" parameter (e.g. "_target1").
* Subclasses can override this for customized target page determination.
* @param request current HTTP request
* @param currentPage the current page, to be returned as fallback
* if no target page specified
* @return the page specified in the request, or current page if not found
* @see #PARAM_TARGET
*/
protected int getTargetPage(HttpServletRequest request, int currentPage) {
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
if (paramName.startsWith(PARAM_TARGET)) {
for (int i = 0; i < WebUtils.SUBMIT_IMAGE_SUFFIXES.length; i++) {
String suffix = WebUtils.SUBMIT_IMAGE_SUFFIXES[i];
if (paramName.endsWith(suffix)) {
paramName = paramName.substring(0, paramName.length() - suffix.length());
}
}
return Integer.parseInt(paramName.substring(PARAM_TARGET.length()));
}
}
return currentPage;
}
/**
* Validate all pages and process finish.
* If there are page validation errors, show the corresponding view page.
*/
private ModelAndView validatePagesAndFinish(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors,
int currentPage) throws Exception {
// In case of binding errors -> show current page.
if (errors.hasErrors()) {
return showPage(request, errors, currentPage);
}
if (!suppressValidation(request, command, errors)) {
// In case of remaining errors on a page -> show the page.
for (int page = 0; page < getPageCount(request, command); page++) {
validatePage(command, errors, page, true);
if (errors.hasErrors()) {
return showPage(request, errors, page);
}
}
}
// No remaining errors -> proceed with finish.
return processFinish(request, response, command, errors);
}
/**
* Template method for custom validation logic for individual pages.
* The default implementation calls {@link #validatePage(Object, Errors, int)}.
* <p>Implementations will typically call fine-granular <code>validateXXX</code>
* methods of this instance's Validator, combining them to validation of the
* corresponding pages. The Validator's default <code>validate</code> method
* will not be called by a wizard form controller!
* @param command form object with the current wizard state
* @param errors validation errors holder
* @param page number of page to validate
* @param finish whether this method is called during final revalidation on finish
* (else, it is called for validating the current page)
* @see #validatePage(Object, Errors, int)
* @see org.springframework.validation.Validator#validate
*/
protected void validatePage(Object command, Errors errors, int page, boolean finish) {
validatePage(command, errors, page);
}
/**
* Template method for custom validation logic for individual pages.
* The default implementation is empty.
* <p>Implementations will typically call fine-granular validateXXX methods of this
* instance's validator, combining them to validation of the corresponding pages.
* The validator's default <code>validate</code> method will not be called by a
* wizard form controller!
* @param command form object with the current wizard state
* @param errors validation errors holder
* @param page number of page to validate
* @see org.springframework.validation.Validator#validate
*/
protected void validatePage(Object command, Errors errors, int page) {
}
/**
* Post-process the given page after binding and validation, potentially
* updating its command object. The passed-in request might contain special
* parameters sent by the page.
* <p>Only invoked when displaying another page or the same page again,
* not when finishing or cancelling.
* @param request current HTTP request
* @param command form object with request parameters bound onto it
* @param errors validation errors holder
* @param page number of page to post-process
* @throws Exception in case of invalid state or arguments
*/
protected void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page)
throws Exception {
}
/**
* Template method for processing the final action of this wizard.
* <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.
* <p>You can call the {@link #showPage} method to return back to the wizard,
* in case of last-minute validation errors having been found that you would
* like to present to the user within the original wizard form.
* @param request current HTTP request
* @param response current HTTP response
* @param command form object with the current wizard state
* @param errors validation errors holder
* @return the finish view
* @throws Exception in case of invalid state or arguments
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
* @see #showPage(javax.servlet.http.HttpServletRequest, org.springframework.validation.BindException, int)
*/
protected abstract ModelAndView processFinish(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception;
/**
* Template method for processing the cancel action of this wizard.
* <p>The default implementation throws a ServletException, saying that a cancel
* operation is not supported by this controller. Thus, you do not need to
* implement this template method if you do not support a cancel operation.
* <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 HTTP request
* @param response current HTTP response
* @param command form object with the current wizard state
* @param errors Errors instance containing errors
* @return the cancellation view
* @throws Exception in case of invalid state or arguments
* @see org.springframework.validation.Errors
* @see org.springframework.validation.BindException#getModel
*/
protected ModelAndView processCancel(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
throw new ServletException(
"Wizard form controller class [" + getClass().getName() + "] does not support a cancel operation");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -