java.lang.Objectorg.apache.struts.action.Action
org.apache.struts.actions.DispatchAction
net.jspcontrols.dialogs.actions.SelectAction
An abstract Action that dispatches a browser event to a handler method. This class allows to process a form submission using either regular submit button or an image button. It works with regular links as well.
parameter
attribute of action mapping.Technical details
Unlike DispatchAction
and LookupDispatchAction
classes, which correlate value
attribute of submit
form element with name of a handler method, this class uses name
attribute. This allows to display a user-friendly caption on a submit button
and to change button caption without redeployment.
The subclass must define a map, which correlates event names with method
names, and handler methods themselves. Events names must be different from
names of action form properties. Each method must have the same signature
as execute
method.
By definition, an event is a request parameter, which starts from
a known prefix. The prefix is specified in getInitKey
method
and can be redefined in a subclass. Default prefix is "DIALOG-EVENT".
If you decide to redifine the event prefix, make sure that it does not contain periods. Also, do not use periods for specific event names. Struts may throw an exception taking the name for nested property and trying to set its value.
Usage
Subclass SelectAction
, implement getKeyMethodMap
method, and map specific events to method handlers. Event name must start
with event prefix:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put(getInitKey()+"-ADD", "add");
map.put(getInitKey()+"-DELETE", "delete");
map.put(getInitKey()+"-CREATE", "create");
map.put(getInitKey()+"-LOGIN", "login");
return map;
}
Remember that standard <html:cancel/>
button is implicitly
mapped to cancelled
method. You need to implement this method
to receive cancel events.
Implement handler methods themselves, for example:
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}
Use event names in name
attribute of submit buttons, or
as query paramter for regular links:
<form action="/selecttest.do" method="post">
<input type="submit" name="DIALOG-EVENT-ADD" value="Add item"/>
<input type="submit" name="DIALOG-EVENT-DELETE" value="Delete item"/>
</form>
<input type="image" name="DIALOG-EVENT-LOGIN" src="login.gif" value="Log In">
<a href="/selecttest.do?DIALOG-EVENT-CREATE">Create item</a>
Notes
execute
method.
getKeyMethodMap
, the first one will be returned.
If no corresponding key is found then an exception will be thrown.cancelled
method.unspecified
method
is called. Override this method to handle default submits.
Field Summary | |
protected java.util.Map |
keyMethodMap
Map, which correlates events with handler methods |
Fields inherited from class org.apache.struts.actions.DispatchAction |
clazz, log, messages, methods, types |
Fields inherited from class org.apache.struts.action.Action |
defaultLocale, servlet |
Constructor Summary | |
SelectAction()
|
Method Summary | |
protected void |
buildLookupMap()
Builds an event map. |
org.apache.struts.action.ActionForward |
cancelled(org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
A handler method for standard Struts Cancel button. |
org.apache.struts.action.ActionForward |
execute(org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it). |
org.apache.struts.action.ActionForward |
execute(ISelectAction action,
org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Dispatches to the method on the action instance of
Action class. |
java.lang.String |
getCancelKey()
Returns the cancel key.Override this method if you want to use prefix different from "DIALOG-EVENT-CANCEL" |
java.lang.String |
getDefaultKey()
Name of request parameter, identifying default event. |
java.lang.String |
getInitKey()
Returns the event prefix. |
protected abstract java.util.Map |
getKeyMethodMap()
Provides mapping between event names (keys) and handler methods (values). |
protected java.lang.reflect.Method |
getMethod(java.lang.Object action,
java.lang.String name)
Introspect the given action class to identify a method of the specified name that accepts the same parameter types as the execute
method does. |
protected java.lang.String |
getMethodName(org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String parameter)
Returns the method name, given a request object and event name. |
void |
resolveEvents(org.apache.struts.action.ActionMapping mapping)
Scans <component> elements of struts-config.xml file and looks up for <event> elements, binding browser events to handler methods. |
org.apache.struts.action.ActionForward |
unspecified(org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
A handler method for "default submit" situation, that is when an event is not recognized in a request. |
Methods inherited from class org.apache.struts.actions.DispatchAction |
dispatchMethod, getMethod |
Methods inherited from class org.apache.struts.action.Action |
addErrors, addMessages, execute, generateToken, getDataSource, getDataSource, getErrors, getLocale, getMessages, getResources, getResources, getServlet, isCancelled, isTokenValid, isTokenValid, resetToken, saveErrors, saveErrors, saveErrors, saveMessages, saveMessages, saveToken, setLocale, setServlet |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected java.util.Map keyMethodMap
Constructor Detail |
public SelectAction()
Method Detail |
public void resolveEvents(org.apache.struts.action.ActionMapping mapping)
Scans <component> elements of struts-config.xml file and looks up for <event> elements, binding browser events to handler methods. If such declarations found, they are used to build handler mapping, and getKeyMethodMap is not called
Therefore, event mappings have to be defined either declaratively or programmatically but not both.
mapping
- ActionMapping from current requestprotected void buildLookupMap() throws javax.servlet.ServletException
javax.servlet.ServletException
public java.lang.String getInitKey()
public java.lang.String getDefaultKey()
unspecified
method is called.
public java.lang.String getCancelKey()
public org.apache.struts.action.ActionForward execute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.lang.Exception
ActionForward
instance describing where and how
control should be forwarded, or null
if the response has
already been completed.
This method should not normally be redefined by a subclass.
mapping
- The ActionMapping used to select this instanceform
- The optional ActionForm bean for this request (if any)request
- The HTTP request we are processingresponse
- The HTTP response we are creating
java.lang.Exception
- if an exception occurspublic org.apache.struts.action.ActionForward execute(ISelectAction action, org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.lang.Exception
action
instance of
Action class. This method should be used when SelectAction is used
as utility class. Always create a separate instance of utility
SelectAction class for each of your custom action classes.
This method uses portions of code from DispatchAction
class.
action
- instance of Action class, method of which is about
to be calledmapping
- Struts action mappingform
- Struts form bean instancerequest
- request we are processingresponse
- response we are returning
execute
method.
java.lang.Exception
protected java.lang.reflect.Method getMethod(java.lang.Object action, java.lang.String name) throws java.lang.NoSuchMethodException
execute
method does.
This method should not be called directly from user code. This method is used internally by SelectAction when it is used as a helper dispatcher, instantiated in a custom action class.
action
- Action class that should be introspectedname
- Name of the method to be introspected
java.lang.NoSuchMethodException
- if no such method can be foundprotected java.lang.String getMethodName(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String parameter) throws java.lang.Exception
mapping
- The ActionMapping used to select this instanceform
- The optional ActionForm bean for this request (if any)request
- The HTTP request we are processingresponse
- The HTTP response we are creatingparameter
- The ActionMapping
parameter's name;
not used by SelectAction
java.lang.Exception
public org.apache.struts.action.ActionForward unspecified(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.lang.Exception
Override this method to provide different behavior.
unspecified
in interface ISelectAction
java.lang.Exception
public org.apache.struts.action.ActionForward cancelled(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.lang.Exception
Override this method to provide appropriate behavior.
cancelled
in interface ISelectAction
java.lang.Exception
protected abstract java.util.Map getKeyMethodMap()
execute
method. To handle
<html:cancel/>
override cancelled
method.
This method must be implemented by a subclass.