org.springframework.webflow.action.AbstractAction Maven / Gradle / Ivy
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.webflow.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ClassUtils;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
/**
* Base action that provides assistance commonly needed by action implementations. This includes:
*
* - Implementing {@link InitializingBean} to receive an init callback when deployed within a Spring bean factory.
*
- Exposing convenient event factory methods to create common result {@link Event} objects such as "success" and
* "error".
*
- A hook for inserting action pre and post execution logic.
*
*
* @author Keith Donald
* @author Erwin Vervaet
*/
public abstract class AbstractAction implements Action, InitializingBean {
/**
* Logger, usable in subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
/**
* Returns the helper delegate for creating action execution result events.
* @return the event factory support
*/
public EventFactorySupport getEventFactorySupport() {
return new EventFactorySupport();
}
public void afterPropertiesSet() throws Exception {
try {
initAction();
} catch (Exception ex) {
throw new BeanInitializationException("Initialization of this Action failed: " + ex.getMessage(), ex);
}
}
/**
* Action initializing callback, may be overridden by subclasses to perform custom initialization logic.
*
* Keep in mind that this hook will only be invoked when this action is deployed in a Spring application context
* since it uses the Spring {@link InitializingBean} mechanism to trigger action initialisation.
*/
protected void initAction() throws Exception {
}
/**
* Returns a "success" result event.
*/
protected Event success() {
return getEventFactorySupport().success(this);
}
/**
* Returns a "success" result event with the provided result object as a parameter.
* @param result the action success result
*/
protected Event success(Object result) {
return getEventFactorySupport().success(this, result);
}
/**
* Returns an "error" result event.
*/
protected Event error() {
return getEventFactorySupport().error(this);
}
/**
* Returns an "error" result event caused by the provided exception.
* @param e the exception that caused the error event, to be configured as an event attribute
*/
protected Event error(Exception e) {
return getEventFactorySupport().error(this, e);
}
/**
* Returns a "yes" result event.
*/
protected Event yes() {
return getEventFactorySupport().yes(this);
}
/**
* Returns a "no" result event.
*/
protected Event no() {
return getEventFactorySupport().no(this);
}
/**
* Returns yes() if the boolean result is true, no() if false.
* @param booleanResult the boolean
* @return yes or no
*/
protected Event result(boolean booleanResult) {
return getEventFactorySupport().event(this, booleanResult);
}
/**
* Returns a result event for this action with the specified identifier. Typically called as part of return, for
* example:
*
*
* protected Event doExecute(RequestContext context) {
* // do some work
* if (some condition) {
* return result("success");
* } else {
* return result("error");
* }
* }
*
*
* Consider calling the error() or success() factory methods for returning common results.
* @param eventId the result event identifier
* @return the action result event
*/
protected Event result(String eventId) {
return getEventFactorySupport().event(this, eventId);
}
/**
* Returns a result event for this action with the specified identifier and the specified set of attributes.
* Typically called as part of return, for example:
*
*
* protected Event doExecute(RequestContext context) {
* // do some work
* AttributeMap resultAttributes = new AttributeMap();
* resultAttributes.put("name", "value");
* if (some condition) {
* return result("success", resultAttributes);
* } else {
* return result("error", resultAttributes);
* }
* }
*
*
* Consider calling the error() or success() factory methods for returning common results.
* @param eventId the result event identifier
* @param resultAttributes the event attributes
* @return the action result event
*/
protected Event result(String eventId, AttributeMap