Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Set the mapping name 

                         

3. Set the mapping name to the action attribute of html:link 

// mappingdispatchaction.jsp 

<tr> 

<td> 

<html:link action="createMappingDispatchAction" ><b>C</b>reate an Employee record</html:link> 

</td> 

</tr> 

<tr><td>&nbsp;</td></tr> 

<tr> 

<td> 

<html:link action="readMappingDispatchAction" ><b>R</b>ead an Employee details</html:link> 

</td> 

</tr> 

<tr><td>&nbsp;</td></tr> 

<tr> 

<td> 

<html:link action="updateMappingDispatchAction" ><b>U</b>pdate Employee details</html:link> 

</td> 

</tr> 

<tr><td>&nbsp;</td></tr> 

<tr> 

<td> 

<html:link action="deleteMappingDispatchAction" ><b>D</b>elete Employee record</html:link> 

</td> 

</tr> 

What are the advantages? 

  1. Single action class can be used for multiple action mappings. The attribute of the action mapping can be added or removed and a new mapping can be created while the action class remains the same. 
  2. Useful in places where there are 2 actions, one of which needs a formbean while the other does not but both uses the same Action class. In such cases, two new action mappings can be created one with ‘name' attribute and one without and the same Action class name can be used. 

What are the disadvantages? 

  1. We still cannot extend our CRUDMappingDispatchAction from any custom base class and have the aggregation intact. 
  2. Cannot attach events. More about this later. 

ActionDispatcher 

How to create one? 

1. Extend your action class from Action. Do not confuse about the inclusion of BaseAction.java. I have just moved the common code part to this BaseAction and extended my action class from it. 

// src/CRUDActionDispatcher.java 

/** 

* This is an example class for demonstrating ActionDispatcher 

* functionality. Observe here that we are extending our 

* action class from BaseAction where the session validation 

* is done. 

* <p> 

* See BaseAction for more details 

* <p> 

* If we use any other aggregating action then we cannot 

* use session validation as we cannot extend our custom class 

* (BaseAction in this case). This is the biggest strength of ActionDispatcher 

* type of aggregation and the biggest weakness of all other types. 



* @author Praveen Babu Kusuma 

* @version 1.0.0 



* http://www.javahome.co.nr 

* http://praveen.awardspace.com 

*/ 

public final class CRUDActionDispatcher extends BaseAction { 

public ActionForward create(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) 

throws Exception { 

// Code for Create Employee follows 

System.out.println("I am in CRUDActionDispatcher - create"); 

return ( mapping.findForward("success") ); 



... 

 

// src/BaseAction.java 

/** 

* This is an example class where session validation 

* is done normally. But if you look at closely we have 

* actually written our ActionDispatcher code here and 

* simply extended our example class for demonstrating 

* ActionDispatcher functionality from this class. 

* <p> 

* This is the place where every time the session is 

* checked. A sample code snippet could be : 



* <code> 

* session = request.getSession(false); // which returns the existing session 

* if( session != null ) { 

* // allow the user in 

* } else { 

* // throw him/her out to the relogin page 

* } 

* </code> 

* <p> 

* This is perhaps the most crucial part of any web application 

* since this is to be done for every class or every class is to be 

* extended from this kind of (Base) class. 



* @author Praveen Babu Kusuma 

* @version 1.0.0 



* http://www.javahome.co.nr 

* http://praveen.awardspace.com 

*/ 

public class BaseAction extends Action { 

protected ActionDispatcher dispatcher = 

new ActionDispatcher(this, ActionDispatcher.DEFAULT_FLAVOR); 

public ActionForward execute(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) 

throws Exception { 

// This is a default method and is mandatory since we are 

// not extending any dispatcher action class(eg, extends ActionDispatcher{ }). 

// This method will handle the dispatching of the action to the appropriate method. 

// Code for Session Validation follows 

System.out.println("I am in BaseAction - execute"); 

return dispatcher.execute(mapping, form, request, response); 



2. Declare a variable of type ActionDispatcher as follow (see above code snippet): 

protected ActionDispatcher dispatcher =  new ActionDispatcher(this, ActionDispatcher.DEFAULT_FLAVOR); 

3. Add the following line as the last line of your default ‘execute' method. Note that this is not needed for other methods. (see above code snippet) 

return dispatcher.execute(mapping, form, request, response); 

4. Define a query string parameter or a hidden variable (‘methodToCall' in our examples) 

// actiondispatcher.jsp 

<tr> 

<td> 

<html:link action="crudActionDispatcher? methodToCall =create"><b>C</b>reate an Employee record</html:link> 

</td> 

</tr> 

5. Set the parameter's value to the desired function name of the action class (create, read etc). 

// actiondispatcher.jsp 

<tr> 

<td> 

<html:link action="crudActionDispatcher? methodToCall=create "><b>C</b>reate an Employee record</html:link> 

</td> 

</tr> 
// actiondispatcher.jsp 

<tr> 

<td> 

<html:link action="crudActionDispatcher? methodToCall =create"><b>C</b>reate an Employee record</html:link> 

</td> 

</tr> 

What are the advantages? 

  1. Observe that we have extended our action class (CRUDActionDispatcher) from our custom base class (BaseClass) which is what we wanted in order to decouple the session validation code into the BaseClass. 
  2. Only a couple of changes to the code avoid the need to extend from a subclass. 
  3. Easy to use. 

What are the disadvantages? 

  1. We cannot attach events to the controls.

Event Aggregating Actions 

If you are familiar with swing programming you might have heard of events. An event is nothing but a piece of code that is executed when, say, a button is clicked, a linked is clicked etc. In all the above types of actions we have defined a parameter (methodToCall) which is in turn mapped to the action name. This is a good feature for security concerns. However, we need a way to have the method name different from the actual method called, at the same time eliminating the need to define a hidden variable or a query string. This is where the Event Aggregating Actions comes into the picture. 

There are two important types of Event Aggregating Actions as said earlier, namely, EventDispatchAction and EventActionDispatcher 

Let's see each one of them in detail. 

EventDispatchAction 

How to create one? 

1. Extend your action from EventDispatchAction 

// CRUDEventDispatchAction.java 

/** 

* This is an example class for demonstrating EventDispatchAction 

* 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 CRUDEventDispatchAction extends EventDispatchAction { 

public ActionForward create(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) throws Exception { 

// Code for Create Employee follows 

System.out.println("I am in CRUDEventDispatchAction - create"); 

return ( mapping.findForward("success") ); 



... 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.