📄 abstractwizardformcontroller.java
字号:
request.getSession().setAttribute(getPageSessionAttributeName(), new Integer(page));
// set page request attribute for evaluation by views
Map controlModel = new HashMap();
if (this.pageAttribute != null) {
controlModel.put(this.pageAttribute, new Integer(page));
}
return showForm(request, errors, this.pages[page], controlModel);
}
else {
throw new ServletException("Invalid page number: " + page);
}
}
/**
* Return the initial page of the wizard, i.e. the page shown at wizard startup.
* Default implementation delegates to 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, i.e. the page shown at wizard startup.
* 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 session attribute that holds
* the page object for this controller.
* @return the name of the page session attribute
*/
protected final String getPageSessionAttributeName() {
return getClass() + ".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>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 null 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);
request.getSession().removeAttribute(getPageSessionAttributeName());
// cancel?
if (isCancel(request)) {
logger.debug("Cancelling wizard for form bean '" + getCommandName() + "'");
return processCancel(request, response, command, errors);
}
// finish?
if (isFinish(request)) {
logger.debug("Finishing wizard for form bean '" + getCommandName() + "'");
return validatePagesAndFinish(request, response, command, errors);
}
// normal submit: validate current page and show specified target page
logger.debug("Validating wizard page " + currentPage + " for form bean '" + getCommandName() + "'");
validatePage(command, errors, currentPage);
int targetPage = getTargetPage(request, command, errors, currentPage);
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.
* Can also be called by page-specific onBindAndValidate implementations,
* as methods like validatePage explicitly feature a page parameter.
* <p>The default implementation checks the page session attribute.
* Subclasses can override this for customized target page determination.
* @throws IllegalStateException if the page attribute isn't in the session
* anymore, i.e. when called after processFormSubmission.
* @see #getPageSessionAttributeName
*/
protected int getCurrentPage(HttpServletRequest request) throws IllegalStateException {
Integer pageAttr = (Integer) request.getSession().getAttribute(getPageSessionAttributeName());
if (pageAttr == null) {
throw new IllegalStateException("Page attribute isn't in session anymore - called after processFormSubmission?");
}
return pageAttr.intValue();
}
/**
* Return if finish action is specified in the request.
* @param request current HTTP request
*/
protected boolean isFinish(HttpServletRequest request) {
return WebUtils.hasSubmitParameter(request, PARAM_FINISH);
}
/**
* Return if cancel action is specified in the request.
* @param request current HTTP request
*/
protected boolean isCancel(HttpServletRequest request) {
return WebUtils.hasSubmitParameter(request, PARAM_CANCEL);
}
/**
* Return the target page specified in the request.
* <p>Default implementation delegates to 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>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 respective view page.
*/
private ModelAndView validatePagesAndFinish(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception {
for (int page = 0; page < pages.length; page++) {
validatePage(command, errors, page);
// in case of field errors on a page -> show the page
if (errors.getErrorCount() - errors.getGlobalErrorCount() > 0) {
return showPage(request, errors, page);
}
}
// no field errors -> maybe global errors, or none at all
return processFinish(request, response, command, errors);
}
/**
* Template method for custom validation logic for individual pages.
* Implementations will typically call fine-granular validateXXX methods of this
* instance's validator, combining them to validation of the respective pages.
* The validator's default validate method will not be called by a wizard controller!
* @param command form object with the current wizard state
* @param errors validation errors holder
* @param page number of page to show
*/
protected abstract void validatePage(Object command, Errors errors, int page);
/**
* Template method for processing the final action of this wizard.
* <p>Can call errors.getModel() to populate the ModelAndView model with the command
* and the Errors instance, under the specified bean name.
* @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
*/
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>Can call errors.getModel() to populate the ModelAndView model with the command
* and the Errors instance, under the specified bean name.
* @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 finish view
* @throws Exception in case of invalid state or arguments
* @see org.springframework.validation.Errors
*/
protected abstract ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -