📄 abstractwizardformcontroller.java
字号:
* @param page number of page to show
* @return the prepared form view
* @throws Exception in case of invalid state or arguments
*/
protected final ModelAndView showPage(RenderRequest request, BindException errors, int page)
throws Exception {
if (page >= 0 && page < getPageCount(request, errors.getTarget())) {
if (logger.isDebugEnabled()) {
logger.debug("Showing wizard page " + page + " for form bean '" + getCommandName() + "'");
}
// Set page session attribute, expose overriding request attribute.
Integer pageInteger = new Integer(page);
String pageAttrName = getPageSessionAttributeName(request);
if (isSessionForm()) {
if (logger.isDebugEnabled()) {
logger.debug("Setting page session attribute [" + pageAttrName + "] to: " + pageInteger);
}
request.getPortletSession().setAttribute(pageAttrName, pageInteger);
}
request.setAttribute(pageAttrName, pageInteger);
// Set page request attribute for evaluation by views.
Map controlModel = new HashMap();
if (this.pageAttribute != null) {
controlModel.put(this.pageAttribute, new Integer(page));
}
String viewName = getViewName(request, errors.getTarget(), page);
return showForm(request, errors, viewName, controlModel);
}
else {
throw new PortletException("Invalid wizard page number: " + page);
}
}
/**
* Return the page count for this wizard form controller.
* Default implementation delegates to <code>getPageCount()</code>.
* <p>Can be overridden to dynamically adapt the page count.
* @param request current portlet request
* @param command the command object as returned by formBackingObject
* @return the current page count
* @see #getPageCount
*/
protected int getPageCount(PortletRequest request, Object command) {
return getPageCount();
}
/**
* Return the name of the view for the specified page of this wizard form controller.
* Default implementation takes the view name from the <code>getPages()</code> array.
* <p>Can be overridden to dynamically switch the page view or to return view names
* for dynamically defined pages.
* @param request current portlet request
* @param command the command object as returned by formBackingObject
* @return the current page count
* @see #getPageCount
*/
protected String getViewName(PortletRequest request, Object command, int page) {
return getPages()[page];
}
/**
* Return the initial page of the wizard, i.e. the page shown at wizard startup.
* Default implementation delegates to <code>getInitialPage(PortletRequest)</code>.
* @param request current portlet request
* @param command the command object as returned by formBackingObject
* @return the initial page number
* @see #getInitialPage(PortletRequest)
* @see #formBackingObject
*/
protected int getInitialPage(PortletRequest 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 portlet request
* @return the initial page number
*/
protected int getInitialPage(PortletRequest request) {
return 0;
}
/**
* Return the name of the PortletSession attribute that holds the page object
* for this wizard form controller.
* <p>Default implementation delegates to the <code>getPageSessionAttributeName</code>
* version without arguments.
* @param request current portlet request
* @return the name of the form session attribute, or null if not in session form mode
* @see #getPageSessionAttributeName
* @see #getFormSessionAttributeName
* @see javax.portlet.PortletSession#getAttribute
*/
protected String getPageSessionAttributeName(PortletRequest request) {
return getPageSessionAttributeName();
}
/**
* Return the name of the PortletSession 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.portlet.PortletSession#getAttribute
*/
protected String getPageSessionAttributeName() {
return getClass().getName() + ".PAGE." + getCommandName();
}
/**
* Pass the page number to the render phase by setting a render parameter.
* This method may not be called when the action calls
* {@link javax.portlet.ActionResponse#sendRedirect(String)}.
* @param response the current action response
* @param page the page number
* @see ActionResponse#setRenderParameter
*/
protected void setPageRenderParameter(ActionResponse response, int page) {
if (logger.isDebugEnabled())
logger.debug("Setting page number render parameter [" + PARAM_PAGE + "] to [" + page + "]");
try {
response.setRenderParameter(PARAM_PAGE, new Integer(page).toString());
}
catch (IllegalStateException ex) {
// ignore in case sendRedirect was already set
}
}
/**
* Pass the the parameter that indicates the target page of the request
* forward to the render phase. If the <code>getTargetPage<code> method
* was overridden, this may need to be overriden as well.
* @param request the current action request
* @param response the current action response
* @see #PARAM_TARGET
* @see #getTargetPage(PortletRequest, int)
* @see #getTargetPage(PortletRequest, Object, Errors, int)
* @see ActionResponse#setRenderParameter
*/
protected void setTargetRenderParameter(ActionRequest request, ActionResponse response) {
try {
Iterator it = PortletUtils.getParametersStartingWith(request, PARAM_TARGET).entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String param = PARAM_TARGET + (String) entry.getKey();
Object value = entry.getValue();
if (logger.isDebugEnabled()) {
logger.debug("Setting target render parameter [" + param + "]");
}
if (value instanceof String) {
response.setRenderParameter(param, (String) value);
}
else if (value instanceof String[]) {
response.setRenderParameter(param, (String[]) value);
}
}
}
catch (IllegalStateException ex) {
// ignore in case sendRedirect was already set
}
}
/**
* Pass the the parameter that indicates a finish request forward to the
* render phase. If the <code>isFinishRequest</code> method
* was overridden, this may need to be overriden as well.
* @param request the current action request
* @param response the current action response
* @see #PARAM_FINISH
* @see #isFinishRequest
* @see ActionResponse#setRenderParameter
*/
protected void setFinishRenderParameter(ActionRequest request, ActionResponse response) {
if (logger.isDebugEnabled())
logger.debug("Setting cancel render parameter [" + PARAM_FINISH + "]");
try {
String name = PortletUtils.getSubmitParameter(request, PARAM_FINISH);
if (name != null)
response.setRenderParameter(name, request.getParameter(name));
}
catch (IllegalStateException ex) {
// ignore in case sendRedirect was already set
}
}
/**
* Pass the the parameter that indicates a cancel request forward to the
* render phase. If the <code>isCancelRequest</code> method
* was overridden, this may need to be overriden as well.
* @param request the current action request
* @param response the current action response
* @see #PARAM_CANCEL
* @see #isCancelRequest
* @see ActionResponse#setRenderParameter
*/
protected void setCancelRenderParameter(ActionRequest request, ActionResponse response) {
if (logger.isDebugEnabled())
logger.debug("Setting cancel render parameter [" + PARAM_CANCEL + "]");
try {
String name = PortletUtils.getSubmitParameter(request, PARAM_CANCEL);
if (name != null)
response.setRenderParameter(name, request.getParameter(name));
}
catch (IllegalStateException ex) {
// ignore in case sendRedirect was already set
}
}
/**
* 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 portlet render request
* @param response current portlet render response
* @return a prepared view, or null if handled directly
* @throws Exception in case of errors
* @see #showNewForm
* @see #setBindOnNewForm
* @see #handleInvalidSubmit
*/
protected ModelAndView renderInvalidSubmit(RenderRequest request, RenderResponse response)
throws Exception {
return showNewForm(request, response);
}
/**
* 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, so here in the action phase this method does nothing. If you
* want to take some action on an invalid submit, you need to override this method.
* @param request current portlet action request
* @param response current portlet action response
* @throws Exception in case of errors
* @see #renderInvalidSubmit
*/
protected void handleInvalidSubmit(ActionRequest request, ActionResponse response) throws Exception {
}
/**
* Apply wizard workflow: finish, cancel, page change.
* @see #processFormSubmission
*/
protected final ModelAndView renderFormSubmission(RenderRequest request, RenderResponse response, Object command, BindException errors)
throws Exception {
int currentPage = getCurrentPage(request);
String pageAttrName = getPageSessionAttributeName(request);
request.setAttribute(pageAttrName, new Integer(currentPage));
// cancel?
if (isCancelRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Cancelling wizard for form bean '" + getCommandName() + "'");
}
return renderCancel(request, response, command, errors);
}
// finish?
if (isFinishRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Finishing wizard for form bean '" + getCommandName() + "'");
}
return renderValidatePagesAndFinish(request, response, command, errors, currentPage);
}
// Normal submit: show specified target page.
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);
}
/**
* Apply wizard workflow: finish, cancel, page change.
* @see #renderFormSubmission
*/
protected final void processFormSubmission(
ActionRequest request, ActionResponse 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.getPortletSession().removeAttribute(pageAttrName);
}
request.setAttribute(pageAttrName, new Integer(currentPage));
// cancel?
if (isCancelRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Cancelling wizard for form bean '" + getCommandName() + "'");
}
setPageRenderParameter(response, currentPage);
setCancelRenderParameter(request, response);
processCancel(request, response, command, errors);
return;
}
// finish?
if (isFinishRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Finishing wizard for form bean '" + getCommandName() + "'");
}
if (!isRedirectAction()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -