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

📄 formaction.java

📁 spring的WEB开发插件,支持多状态WEB开发
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	/**
	 * Get the scope in which the form object will be placed. Can be either flow
	 * scope or request scope. Defaults to request scope.
	 */
	public ScopeType getFormObjectScope() {
		return this.formObjectScope;
	}

	/**
	 * Set the scope in which the form object will be placed. Can be either flow
	 * scope or request scope.
	 */
	public void setFormObjectScope(ScopeType scopeType) {
		this.formObjectScope = scopeType;
	}

	/**
	 * Convenience setter that performs a string to ScopeType conversion for
	 * you.
	 * @param encodedScopeType the encoded scope type string
	 * @throws InvalidFormatException the encoded value was invalid
	 */
	public void setFormObjectScopeAsString(String encodedScopeType) throws InvalidFormatException {
		this.formObjectScope = (ScopeType)new LabeledEnumFormatter().parseValue(encodedScopeType, ScopeType.class);
	}

	/**
	 * Get the scope in which the Errors object will be placed. Can be either
	 * flow scope ore request scope. Defaults to request scope.
	 */
	public ScopeType getFormErrorsScope() {
		return formErrorsScope;
	}

	/**
	 * Set the scope in which the Errors object will be placed. Can be either
	 * flow scope ore request scope. Defaults to request scope.
	 */
	public void setFormErrorsScope(ScopeType errorsScope) {
		this.formErrorsScope = errorsScope;
	}

	/**
	 * Convenience setter that performs a string to ScopeType conversion for
	 * you.
	 * @param encodedScopeType the encoded scope type string
	 * @throws InvalidFormatException the encoded value was invalid
	 */
	public void setFormErrorsScopeAsString(String encodedScopeType) throws InvalidFormatException {
		this.formErrorsScope = (ScopeType)new LabeledEnumFormatter().parseValue(encodedScopeType, ScopeType.class);
	}

	/**
	 * Get the property editor registration strategy for this action's data
	 * binders.
	 */
	public PropertyEditorRegistrar getPropertyEditorRegistrar() {
		return propertyEditorRegistrar;
	}

	/**
	 * Set a property editor registration strategy for this action's data
	 * binders. This is an alternative to overriding the initBinder() method.
	 */
	public void setPropertyEditorRegistrar(PropertyEditorRegistrar propertyEditorRegistrar) {
		this.propertyEditorRegistrar = propertyEditorRegistrar;
	}

	/**
	 * Returns the validator for this action.
	 */
	public Validator getValidator() {
		return validator;
	}

	/**
	 * Set the validator for this action. The validator must support the
	 * specified form object class.
	 */
	public void setValidator(Validator validator) {
		this.validator = validator;
		getValidateMethodDispatcher().setTarget(validator);
	}

	/**
	 * Returns if event parameters should be bound to the form object during the
	 * {@link #setupForm(RequestContext)} action.
	 * @return bind on setup form
	 */
	public boolean isBindOnSetupForm() {
		return this.bindOnSetupForm;
	}
	
	/**
	 * Set if event parameters should be bound to the form object during the
	 * {@link #setupForm(RequestContext)} action.
	 */
	public void setBindOnSetupForm(boolean bindOnSetupForm) {
		this.bindOnSetupForm = bindOnSetupForm;
	}

	/**
	 * Return if the validator should get applied when binding. Defaults to
	 * true.
	 */
	public boolean isValidateOnBinding() {
		return validateOnBinding;
	}

	/**
	 * Set if the validator should get applied when binding.
	 */
	public void setValidateOnBinding(boolean validateOnBinding) {
		this.validateOnBinding = validateOnBinding;
	}

	/**
	 * Return the strategy to use for resolving errors into message codes.
	 */
	public MessageCodesResolver getMessageCodesResolver() {
		return messageCodesResolver;
	}

	/**
	 * Set the strategy to use for resolving errors into message codes. Applies
	 * the given strategy to all data binders used by this action.
	 * <p>
	 * Default is null, i.e. using the default strategy of the data binder.
	 * @see #createBinder(RequestContext, Object)
	 * @see org.springframework.validation.DataBinder#setMessageCodesResolver(org.springframework.validation.MessageCodesResolver)
	 */
	public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) {
		this.messageCodesResolver = messageCodesResolver;
	}

	protected void initAction() {
		if (getValidator() != null) {
			if (getFormObjectClass() != null && !getValidator().supports(getFormObjectClass())) {
				throw new IllegalArgumentException("Validator [" + getValidator()
						+ "] does not support form object class [" + getFormObjectClass() + "]");
			}
		}
	}

	// action execute methods

	/**
	 * Loads the form object and ensures it is exposed in the model of the executing flow in 
	 * the correct scope.
	 * <p>
	 * This is a fine-grained action method that you may invoke and combine with other action
	 * methods as part of a chain.  For example, one could call "exposeFormObject" and then
	 * "bind" to achieve setupForm-like behaviour, with the ability to respond to results of
	 * each actions independently as part of a flow definition.
	 * <p>
	 * Here is that example of that 'action chaining' illustrated:
	 * <pre>
	 * &lt;action-state method="setupForm"&gt;
	 *     &lt;action name="exposer" bean="formAction" method="exposeFormObject"/&gt;
	 *     &lt;action bean="formAction" method="bind"/&gt;
	 *     &lt;transition on="exposer.error" to="displayFormObjectRetrievalFailurePage"/&gt;
	 *     &lt;transition on="success" to="displayForm"/&gt;
	 * &lt;/action-state&gt;
	 * </pre>
	 * @param context the flow request context
	 * @return "success" if the action completed successsfully, "error" otherwise
	 * @throws Exception an unrecoverable exception occured
	 */
	public Event exposeFormObject(RequestContext context) throws Exception {
		try {
			Object formObject = getFormObject(context);
			setFormObject(context, formObject);
			ensureFormErrorsExposed(context, formObject);
			return success();
		}
		catch (FormObjectRetrievalFailureException e) {
			return error(e);
		}		
	}
	
	/**
	 * Prepares a form object for display in a new form. This will initialize
	 * the binder so that all custom property editors are available for use in
	 * the new form.
	 * <p>
	 * If the setupBindingEnabled method returns true a data binding operation
	 * will occur to pre-populate the new form with incoming event parameters.
	 * @param context the action execution context, for accessing and setting
	 *        data in "flow scope" or "request scope"
	 * @return "success" when binding and validation is successful, "error"
	 *         if there were binding or validation errors or the form object
	 *         could not be retrieved
	 * @throws Exception an <b>unrecoverable</b> exception occured, either
	 *         checked or unchecked
	 */
	public Event setupForm(RequestContext context) throws Exception {
		Object formObject = null;
		try {
			formObject = getFormObject(context);
		}
		catch (FormObjectRetrievalFailureException e) {
			// handle retrieval exceptions only, other exceptions propagate
			return error(e);
		}
		setFormObject(context, formObject);
		if (setupBindingEnabled(context)) {
			DataBinder binder = createBinder(context, formObject);
			doBind(context, binder);
			setFormErrors(context, binder.getErrors());
			return binder.getErrors().hasErrors() ? error() : success();
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug("Setup form object binding was disabled for this request");
			}
			ensureFormErrorsExposed(context, formObject);
			return success();
		}
	}
	
	/**
	 * Bind all incoming request parameters to the form object and validate the
	 * form object using a registered validator.
	 * @param context the action execution context, for accessing and setting
	 *        data in "flow scope" or "request scope"
	 * @return "success" when binding and validation is successful, "error"
	 *         if ther were errors or the form object could not be retrieved
	 * @throws Exception an <b>unrecoverable</b> exception occured, either
	 *         checked or unchecked
	 */
	public Event bindAndValidate(RequestContext context) throws Exception {
		Object formObject = null;
		try {
			formObject = getFormObject(context);
		}
		catch (FormObjectRetrievalFailureException e) {
			// handle retrieval exceptions only, other exceptions propagate
			return error(e);
		}
		setFormObject(context, formObject);
		DataBinder binder = createBinder(context, formObject);
		doBind(context, binder);
		setFormErrors(context, binder.getErrors());
		if (getValidator() != null && isValidateOnBinding() && validationEnabled(context)) {
			doValidate(context, binder);
		}
		else {
			if (logger.isDebugEnabled()) {
				if (validator == null) {
					logger.debug("No validator is configured: no validation will occur");
				}
				else {
					logger.debug("Validation was disabled for this request");
				}
			}
		}
		return binder.getErrors().hasErrors() ? error() : success();		
	}

	/**
	 * Bind the parameters of the last event in the request context to the
	 * form object.
	 * @param context the action execution context, for accessing and setting
	 *        data in "flow scope" or "request scope"
	 * @return "success" if there are no binding errors, "error" if there
	 *         are errors or the form object could not be retrieved
	 * @throws Exception an <b>unrecoverable</b> exception occured, either
	 *         checked or unchecked
	 */
	public Event bind(RequestContext context) throws Exception {
		Object formObject = null;
		try {
			formObject = getFormObject(context);
		}
		catch (FormObjectRetrievalFailureException e) {
			// handle retrieval exceptions only, other exceptions propagate
			return error(e);
		}
		setFormObject(context, formObject);
		DataBinder binder = createBinder(context, formObject);
		doBind(context, binder);
		setFormErrors(context, binder.getErrors());
		return binder.getErrors().hasErrors() ? error() : success();
	}
	
	/**
	 * Validate the form object.
	 * @param context the action execution context, for accessing and setting
	 *        data in "flow scope" or "request scope"
	 * @return "success" if there are no validation errors, "error" if there 
	 *         are errors or the form object could not be retrieved
	 * @throws Exception an <b>unrecoverable</b> exception occured, either
	 *         checked or unchecked
	 */
	public Event validate(RequestContext context) throws Exception {
		Object formObject = null;
		try {
			formObject = getFormObject(context);
		}
		catch (FormObjectRetrievalFailureException e) {
			// handle retrieval exceptions only, other exceptions propagate
			return error(e);
		}
		setFormObject(context, formObject);
		DataBinder binder = createBinder(context, formObject);
		doValidate(context, binder);
		setFormErrors(context, binder.getErrors());

⌨️ 快捷键说明

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