⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractformcontroller.java

📁 spring api 源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * {@link ActionResponse#setRenderParameter} and
	 * {@link ActionResponse#setRenderParameters}.
	 * @param redirectAction true if ActionResponse#sendRedirect is expected to be called
	 * @see ActionResponse#sendRedirect
	 */
	public void setRedirectAction(boolean redirectAction) {
		this.redirectAction = redirectAction;
	}

	/**
	 * Return if {@link ActionResponse#sendRedirect} is
	 * expected to be called in the action phase.
	 */
	public boolean isRedirectAction() {
		return this.redirectAction;
	}

	/**
	 * Specify the list of parameters that should be passed forward
	 * from the action phase to the render phase whenever the form is
	 * rerendered or when {@link #passRenderParameters} is called.
	 * @see #passRenderParameters
	 */
	public void setRenderParameters(String[] parameters) {
		this.renderParameters = parameters;
	}

	/**
	 * Returns the list of parameters that will be passed forward
	 * from the action phase to the render phase whenever the form is
	 * rerendered or when {@link #passRenderParameters} is called.
	 * @return the list of parameters
	 * @see #passRenderParameters
	 */
	public String[] getRenderParameters() {
		return this.renderParameters;
	}


	/**
	 * Handles action phase of two cases: form submissions and showing a new form.
	 * Delegates the decision between the two to <code>isFormSubmission</code>,
	 * always treating requests without existing form session attribute
	 * as new form when using session form mode.
	 * @see #isFormSubmission
	 * @see #processFormSubmission
	 * @see #handleRenderRequestInternal
	 */
	protected void handleActionRequestInternal(ActionRequest request, ActionResponse response)
			throws Exception {

		// Form submission or new form to show?
		if (isFormSubmission(request)) {
			// Fetch form object, bind, validate, process submission.
			try {
				Object command = getCommand(request);
				if (logger.isDebugEnabled()) {
					logger.debug("Processing valid submit (redirectAction = " + isRedirectAction() + ")");
				}
				if (!isRedirectAction()) {
					setFormSubmit(response);
				}
				PortletRequestDataBinder binder = bindAndValidate(request, command);
				BindException errors = new BindException(binder.getBindingResult());
				processFormSubmission(request, response, command, errors);
				setRenderCommandAndErrors(request, command, errors);
				return;
			}
			catch (PortletSessionRequiredException ex) {
				// Cannot submit a session form if no form object is in the session.
				if (logger.isDebugEnabled()) {
					logger.debug("Invalid submit detected: " + ex.getMessage());
				}
				setFormSubmit(response);
				setInvalidSubmit(response);
				handleInvalidSubmit(request, response);
				return;
			}
		}

		else {
			logger.debug("Not a form submit - passing parameters to render phase");
			passRenderParameters(request, response);
			return;
		}
	}

	/**
	 * Handles render phase of two cases: form submissions and showing a new form.
	 * Delegates the decision between the two to <code>isFormSubmission</code>,
	 * always treating requests without existing form session attribute
	 * as new form when using session form mode.
	 * @see #isFormSubmission
	 * @see #showNewForm
	 * @see #processFormSubmission
	 * @see #handleActionRequestInternal
	 */
	protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response)
			throws Exception {

		// Form submission or new form to show?
		if (isFormSubmission(request)) {

			// If it is an invalid submit then handle it.
			if (isInvalidSubmission(request)) {
				logger.debug("Invalid submit - calling renderInvalidSubmit");
				return renderInvalidSubmit(request, response);
			}

			// Valid submit -> render.
			logger.debug("Valid submit - calling renderFormSubmission");
			return renderFormSubmission(request, response, getRenderCommand(request), getRenderErrors(request));
		}

		else {
			// New form to show: render form view.
			return showNewForm(request, response);
		}
	}

	/**
	 * Determine if the given request represents a form submission.
	 * <p>Default implementation checks to see if this is an ActionRequest
	 * and treats all action requests as form submission. During the action
	 * phase it will pass forward a render parameter to indicate to the render
	 * phase that this is a form submission. This method can check both
	 * kinds of requests and indicate if this is a form submission.
	 * <p>Subclasses can override this to use a custom strategy, e.g. a specific
	 * request parameter (assumably a hidden field or submit button name). Make
	 * sure that the override can handle both ActionRequest and RenderRequest
	 * objects properly.
	 * @param request current request
	 * @return if the request represents a form submission
	 */
	protected boolean isFormSubmission(PortletRequest request) {
		return (request instanceof ActionRequest ? true :
				TRUE.equals(request.getParameter(getFormSubmitParameterName())));
	}

	/**
	 * Determine if the given request represents an invalid form submission.
	 */
	protected boolean isInvalidSubmission(PortletRequest request) {
		return TRUE.equals(request.getParameter(getInvalidSubmitParameterName()));
	}

	/**
	 * Return the name of the render parameter that indicates this
	 * was a form submission.
	 * @return the name of the render parameter
	 * @see javax.portlet.RenderRequest#getParameter
	 */
	protected String getFormSubmitParameterName() {
		return FORM_SUBMISSION_PARAMETER;
	}

	/**
	 * Return the name of the render parameter that indicates this
	 * was an invalid form submission.
	 * @return the name of the render parameter
	 * @see javax.portlet.RenderRequest#getParameter
	 */
	protected String getInvalidSubmitParameterName() {
		return INVALID_SUBMISSION_PARAMETER;
	}

	/**
	 * Set the action response parameter that indicates this in a form submission.
	 * @param response the current action response
	 * @see #getFormSubmitParameterName()
	 */
	protected final void setFormSubmit(ActionResponse response) {
		if (logger.isDebugEnabled()) {
			logger.debug("Setting render parameter [" + getFormSubmitParameterName() +
					"] to indicate this is a form submission");
		}
		try {
			response.setRenderParameter(getFormSubmitParameterName(), TRUE);
		}
		catch (IllegalStateException ex) {
			// Ignore in case sendRedirect was already set.
		}
	}

	/**
	 * Set the action response parameter that indicates this in an invalid submission.
	 * @param response the current action response
	 * @see #getInvalidSubmitParameterName()
	 */
	protected final void setInvalidSubmit(ActionResponse response) {
		if (logger.isDebugEnabled()) {
			logger.debug("Setting render parameter [" + getInvalidSubmitParameterName() +
					"] to indicate this is an invalid submission");
		}
		try {
			response.setRenderParameter(getInvalidSubmitParameterName(), TRUE);
		}
		catch (IllegalStateException ex) {
			// Ignore in case sendRedirect was already set.
		}
	}

	/**
	 * Return the name of the PortletSession attribute that holds the form object
	 * for this form controller.
	 * <p>Default implementation delegates to the <code>getFormSessionAttributeName</code>
	 * version without arguments.
	 * @param request current HTTP request
	 * @return the name of the form session attribute, or null if not in session form mode
	 * @see #getFormSessionAttributeName
	 * @see javax.portlet.PortletSession#getAttribute
	 */
	protected String getFormSessionAttributeName(PortletRequest request) {
		return getFormSessionAttributeName();
	}

	/**
	 * Return the name of the PortletSession attribute that holds the form object
	 * for this 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 form session attribute
	 * @see javax.portlet.PortletSession#getAttribute
	 */
	protected String getFormSessionAttributeName() {
		return getClass().getName() + ".FORM." + getCommandName();
	}

	/**
	 * Pass the specified list of action request parameters to the render phase
	 * by putting them into the action response object. This may not be called
	 * when the action will call will call
	 * {@link ActionResponse#sendRedirect sendRedirect}.
	 * @param request the current action request
	 * @param response the current action response
	 * @see ActionResponse#setRenderParameter
	 */
	protected void passRenderParameters(ActionRequest request, ActionResponse response) {
		if (this.renderParameters == null) {
			return;
		}
		try {
			for (int i = 0; i < this.renderParameters.length; i++) {
				String paramName = this.renderParameters[i];
				String paramValues[] = request.getParameterValues(paramName);
				if (paramValues != null) {
					if (logger.isDebugEnabled()) {
						logger.debug("Passing parameter to render phase '" + paramName + "' = " +
								(paramValues == null ? "NULL" : Arrays.asList(paramValues).toString()));
					}
					response.setRenderParameter(paramName, paramValues);
				}
			}
		}
		catch (IllegalStateException ex) {
			// Ignore in case sendRedirect was already set.
		}
	}


	/**
	 * Show a new form. Prepares a backing object for the current form
	 * and the given request, including checking its validity.
	 * @param request current render request
	 * @param response current render response
	 * @return the prepared form view
	 * @throws Exception in case of an invalid new form object
	 * @see #getErrorsForNewForm
	 */
	protected final ModelAndView showNewForm(RenderRequest request, RenderResponse response)
			throws Exception {

		logger.debug("Displaying new form");
		return showForm(request, response, getErrorsForNewForm(request));
	}

	/**
	 * Create a BindException instance for a new form.
	 * Called by <code>showNewForm</code>.
	 * <p>Can be used directly when intending to show a new form but with
	 * special errors registered on it (for example, on invalid submit).
	 * Usually, the resulting BindException will be passed to
	 * <code>showForm</code>, after registering the errors on it.
	 * @param request current render request
	 * @return the BindException instance
	 * @throws Exception in case of an invalid new form object
	 */
	protected final BindException getErrorsForNewForm(RenderRequest request) throws Exception {
		// Create form-backing object for new form
		Object command = formBackingObject(request);
		if (command == null) {
			throw new PortletException("Form object returned by formBackingObject() must not be null");
		}
		if (!checkCommand(command)) {
			throw new PortletException("Form object returned by formBackingObject() must match commandClass");
		}

		// Bind without validation, to allow for prepopulating a form, and for
		// convenient error evaluation in views (on both first attempt and resubmit).
		PortletRequestDataBinder binder = createBinder(request, command);
		BindException errors = new BindException(binder.getBindingResult());

		if (isBindOnNewForm()) {
			if (logger.isDebugEnabled()) {
				logger.debug("Binding to new form");
			}
			binder.bind(request);
			onBindOnNewForm(request, command, errors);
		}
		
		// Return BindException object that resulted from binding.
		return errors;
	}

	/**
	 * Callback for custom post-processing in terms of binding for a new form.
	 * Called when preparing a new form if <code>bindOnNewForm</code> is <code>true</code>.
	 * <p>Default implementation delegates to <code>onBindOnNewForm(request, command)</code>.
	 * @param request current render request
	 * @param command the command object to perform further binding on
	 * @param errors validation errors holder, allowing for additional
	 * custom registration of binding errors

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -