Set the action name
2. Set the action name to the action attribute of html:link
// eventdispatchaction.jsp
<tr> <td> <html:link action="crudEventDispatchAction ?createAlias=create " ><b>C</b>reate an Employee record</html:link> </td> </tr> |
3. Either directly use the method name to call as a query string parameter, or use an alias.
// eventdispatchaction.jsp ( Direct calling ) <tr> <td> <html:link action="crudEventDispatchAction? update "><b>U</b>pdate Employee details</html:link> </td> </tr> |
// eventdispatchaction.jsp ( Calling using an alias ) <tr> <td> <html:link action="crudEventDispatchAction? createAlias=create" ><b>C</b>reate an Employee record</html:link> </td> </tr> |
What are the advantages?
-
Events can be attached to controls directly.
-
No need to use a separate hidden variable or a query string for mapping the method name to the actual method to call.
-
Actual method to call can be aliased with a pseudo name.
What are the disadvantages?
-
The need to extend the class from EventDispatchAction and hence cannot extend our class from a custom base class.
EventActionDispatcher
How to create one?
-
Extend your action from Action, similar to the case in ActionDispatcher. This is what is desirable for us.
// CRUDEventActionDispatcher.java /** * This is an example class for demonstrating EventActionDispatcher * functionality. Observe here that we can also extend our class from * BaseAction class like in ActionDispatcher example and move our * session validation to that class. * <p> * The EventActionDispatcher as the name suggests is used to attach * events to the html controls. This type of aggregation eliminates the * need to define a parameter(eg., methodToCall). We havent used any * hidden parameters to combine an event to a method. This event-to-method * mapping is removed using EventActionDispatcher. * * We can also use an Alias for the method name and map it to the method name * as can be seen in the createAlias event. * * @author Praveen Babu Kusuma * @version 1.0.0 * * http://www.javahome.co.nr * http://praveen.awardspace.com */ public final class CRUDEventActionDispatcher extends Action { protected ActionDispatcher dispatcher = new EventActionDispatcher(this); public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // This method will handle the dispatching of the action to the appropriate method. System.out.println("I am in CRUDEventActionDispatcher - execute"); return dispatcher.execute(mapping, form, request, response); } ... } |
2. Set the action name to the action attribute of html:link.
// eventactiondispatcher.jsp <tr> <td> <html:link action =" crudEventActionDispatcher ?createAlias=create"><b>C</b>reate an Employee record</html:link> </td> </tr> |
3. Either directly use the method name to call as a query string parameter, or use an alias.
// eventactiondispatcher.jsp ( Direct calling ) <tr> <td> <html:link action="crudEventActionDispatcher? delete "><b>D</b>elete Employee record</html:link> </td> </tr> |
// eventactiondispatcher.jsp ( Calling using an alias ) <tr> <td> <html:link action="crudEventActionDispatcher? createAlias=create "><b>C</b>reate an Employee record</html:link> </td> </tr> |
What are the advantages?
-
Events can be attached to controls directly.
-
No need to use a separate hidden variable or a query string for mapping the method name to the actual method to call.
-
Actual method to call can be aliased with a pseudo name.
What are the disadvantages?
1. None.
Note that the use of true (read=true) while using the query string variable to specify the event name is optional. Also, note that this variable is different from the one we used earlier (methodToCall). Here we directly specify the event name or its alias not the variable name or the actual method to call.
Assumptions
-
It is assumed that you have the complete source code of this discussion with you.
-
For some examples, I assume that our application is a full fledged web application wherein a user is authenticated before entry into the system and we are validating the user session in a base class. Why is this so important? Because, to use some types of aggregations, we should extend our action class from a specific subclass of Action. This restricts us from extending the action class from a custom base class. DispatchAction is an example of such an action.
-
It is assumed that all we are interested in are the Create, Read, Update and Delete operations on an entity in the database. These
actions, also called CRUD actions, are the basis for all the examples of this discussion.
-
One more assumption is that it is assumed that we are interested in attaching events to the HTML controls, which you may be familiar in JSF, where in we attach events to the controls using action and actionListener attributes.
-
It is assumed that the examples provided depict only one way of usage. You are free to experiment with the code, like for instance, replacing query string with hidden variables.
Source Code