org.springframework.webflow.action.MultiAction Maven / Gradle / Ivy
/*
* Copyright 2002-2006 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.springframework.webflow.Event;
import org.springframework.webflow.RequestContext;
import org.springframework.webflow.util.DispatchMethodInvoker;
/**
* Action implementation that bundles two or more action execution methods into
* a single class. Action execution methods defined by subclasses must adhere to
* the following signature:
*
*
* public Event ${method}(RequestContext context) throws Exception;
*
*
* When this action is invoked, by default the id
of the calling
* action state state is treated as the action execution method name.
* Alternatively, the execution method name may be explicitly specified as a
* property of the calling action state.
*
* For example, the following action state definition:
*
*
* <action-state id="search">
* <action bean="searchAction"/>
* <transition on="success" to="results"/>
* </action-state>
*
*
* ... when entered, executes the method:
*
*
* public Event search(RequestContext context) throws Exception;
*
*
* Alternatively you may explictly specify the method name:
*
*
* <action-state id="executingSearch">
* <action bean="phonebook" method="executeSearch"/>
* <transition on="success" to="results"/>
* </action-state>
*
*
*
* A typical use of the MultiAction is to centralize all command logic for a
* flow in one place. Another common use is to centralize form setup and submit
* logic into one place, or CRUD (create/read/update/delete) operations for a
* single domain object in one place.
*
* Exposed configuration properties:
*
*
* Name
* Default
* Description
*
*
* methodResolver
* Treats the id of the "currentState" as the target method name
* Set the strategy used to resolve the name (key) of an action execution
* method. Allows full control over the method resolution algorithm.
*
*
*
* @see MultiAction.MethodResolver
*
* @author Keith Donald
* @author Erwin Vervaet
*/
public class MultiAction extends AbstractAction {
/**
* A cache for dispatched action execute methods. The default signature is
* public Event ${method}(RequestContext context) throws Exception;
.
*/
private DispatchMethodInvoker methodInvoker;
/**
* The action method resolver strategy.
*/
private MethodResolver methodResolver = new DefaultMultiActionMethodResolver();
/**
* Protected default constructor; not invokable by direct MultiAction instantiation.
*/
protected MultiAction() {
setTarget(this);
}
/**
* Constructs a multi action that invokes methods on the specified target
* object. Note: invokable methods on the target must conform to the multi action
* method signature:
*
* public Event ${method}(RequestContext context) throws Exception;
*
* @param target the target of this multi action's invocation
*/
public MultiAction(Object target) {
setTarget(target);
}
/**
* Sets the target of this multi action invocation.
* @param target the target
*/
protected final void setTarget(Object target) {
methodInvoker = new DispatchMethodInvoker(target, new Class[] { RequestContext.class } );
}
/**
* Get the strategy used to resolve action execution method keys.
*/
public MethodResolver getMethodResolver() {
return methodResolver;
}
/**
* Set the strategy used to resolve action execution method keys.
*/
public void setMethodResolver(MethodResolver methodResolver) {
this.methodResolver = methodResolver;
}
protected final Event doExecute(RequestContext context) throws Exception {
String method = getMethodResolver().resolveMethod(context);
return (Event)methodInvoker.invoke(method, new Object[] { context });
}
/**
* Strategy interface used by the MultiAction to map a request context to
* the name (key) of an action execution method.
*
* @author Keith Donald
* @author Erwin Vervaet
*/
public interface MethodResolver {
/**
* Resolve a method key from given flow execution request context.
* @param context the flow execution request context
* @return the key identifying the method that should handle action
* execution
*/
public String resolveMethod(RequestContext context);
}
}