Aggregating Actions In Struts Revisited

In my previous article Aggregating Actions in Struts , I have given a brief idea of how to create action aggregations.


 

Ads

Tutorials   
Struts 2.5.10.1 General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version 2.3.16.3 released Struts 2 version 2.3.15.3 released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts 2.3.15.2 launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts 2.3.15.1 Maven - How to use Maven dependency of Struts 2.3.15.1? Download Struts 2.3.15.1 How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts 2.3.15.1 Struts 2 Tutorials - Struts version 2.3.15.1 Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration Struts2 Tiles Example
Ads

Aggregating Actions In Struts Revisited

     

In my previous article Aggregating Actions in Struts , I have given a brief idea of how to create action aggregations. An action aggregation is a grouping of a set of related actions into a single unified action in order to eliminate the need to write a new action class for every action. In this part, I will walk through a full-fledged practical example of each of the types presented in Part I namely the DispatchAction , LookupDispatchAction , MappingDispatchAction and ActionDispatcher along with two new aggregations which slightly differ in syntax but are more efficient than the previous ones. These are EventDispatchAction and EventActionDispatcher . I call these new actions as ?Event Aggregating Actions? and the ones in Part I as ?Non-Event Aggregating Actions.? 

Now let's go into some details. 

Non-Event Aggregating Actions 

DispatchAction 

How to create one? 

? Extend your action class from DispatchAction. 

// src/CRUDDispatchAction.java 

/** 

* This is an example class for demonstrating DispatchAction 

* functionality. Observe here that we are extending our 

* action class not from Action but from DispatchAction. 



* @author Praveen Babu Kusuma 

* @version 1.0.0 



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

* http://praveen.awardspace.com 

*/ 

public final class CRUDDispatchAction extends DispatchAction { 

public ActionForward create(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) 

throws Exception { 

// Code for Create Employee follows 

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

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



... 

? Define a query string parameter or a hidden variable (?methodToCall' in our examples)

// dispatchaction.jsp 

<tr> 

<td> 

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

</td> 

</tr> 

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

// dispatchaction.jsp 

<tr> 

<td> 

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

</td> 

</tr> 

What are the advantages? 

  1. If you observed carefully, we have created only one action class (CRUDDispatchAction) instead of four different classes for each of the Create, Read, Update and Delete actions. This is why aggregating actions are for. 
  2. Easy to use. 
  3. Cleaner code syntax. 

What are the disadvantages? 

  1. We cannot extend our CRUDDispatchAction from any custom base class and still have the aggregation. 
  2. Observe that we have used html:link in dispatchaction.jsp and a query string variable appended to the URL. So, the method name is visible in the URL. In order to avoid this, we need to set it using javascript. 
  3. Cannot attach events. More about this later. 

LookupDispatchAction 

How to create one? 

1. Extend your action class from LookupDispatchAction . 

// src/CRUDLookupDispatchAction.java 

/** 

* This is an example class for demonstrating CRUDLookupDispatchAction 

* functionality. Observe here that we are extending our 

* action class not from Action but from LookupDispatchAction. 



* @author Praveen Babu Kusuma 

* @version 1.0.0 



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

* http://praveen.awardspace.com 

*/ 

public final class CRUDLookupDispatchAction extends LookupDispatchAction { 

protected Map getKeyMethodMap() { 

Map map = new HashMap(); 

map.put("button.create", "create"); 

map.put("button.read", "read"); 

map.put("button.update", "update"); 

map.put("button.delete", "delete"); 

return map; 



public ActionForward create(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) 

throws Exception { 

// Code for Create Employee follows 

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

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



... 

2. Write an html:form and change the action name to the desired action mapping name. 

// lookupdispatchaction.jsp 

<html:form action="crudLookupDispatchAction" > 

... 

3. Set the ?property' attribute of the submit button to parameter name which should hold the method name to call. 

// lookupdispatchaction.jsp 

<tr> 

<td align="center"> 

<html:submit property="methodToCall" ><bean:message key="button.create"/></html:submit> 

</td> 

</tr> 

What are the advantages? 

  1. Multiple submit buttons within the same form only differing in their name. 
  2. The actual method to call is mapped in the action class. 
  3. Button names can be set in a properties file. 

What are the disadvantages? 

  1. We cannot extend our CRUDLookupDispatchAction from any custom base class and still have the aggregation. 
  2. Change to the button name requires JVM restart. 
  3. Extra overhead of creating a separate method for button-to-method name mapping.(getKeyMethodMap() in our example) 
  4. Cannot attach events. More about this later. 

MappingDispatchAction 

How to create one? 

1. Extend your action class from MappingDispatchAction 

// src/CRUDMappingDispatchAction.java 

/** 

* This is an example class for demonstrating MappingDispatchAction 

* functionality. Observe here that we are extending our 

* action class not from Action but from MappingDispatchAction. 



* @author Praveen Babu Kusuma 

* @version 1.0.0 



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

* http://praveen.awardspace.com 

*/ 

public final class CRUDMappingDispatchAction extends MappingDispatchAction { 

public ActionForward create(ActionMapping mapping, 

ActionForm form, 

HttpServletRequest request, 

HttpServletResponse response) 

throws Exception { 

// Code for Create Employee follows 

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

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



... 

Define different mappings for each action as needed. 

// struts-config.xml 

<!-- MappingDispatchAction mappings Starts --> 

<!-- MappingDispatchAction is used when the mappings differ 

(observe that the number/type of attributes differ for each action) --> 

<action path="/createMappingDispatchAction" 

type="com.CRUDMappingDispatchAction" 

scope="request" 

parameter="create" 



<forward name="success" path="/success.jsp"/> 

</action> 



<action path="/readMappingDispatchAction" 

type="com.CRUDMappingDispatchAction" 

scope="session" 

parameter="read" 



<forward name="success" path="/success.jsp"/> 

</action> 



<action path="/updateMappingDispatchAction" 

type="com.CRUDMappingDispatchAction" 

scope="request" 

input="/mappingdispatchaction.jsp" 

parameter="update" 



<forward name="success" path="/success.jsp"/> 

</action> 



<action path="/deleteMappingDispatchAction" 

type="com.CRUDMappingDispatchAction" 

scope="request" 

name="employeeForm" 

parameter="delete" 

validate="false" 



<forward name="success" path="/success.jsp"/> 

</action> 

<!-- MappingDispatchAction mappings Ends --> 


Advertisements

Share on Google+Share on Google+

Aggregating Actions In Struts Revisited

Posted on: June 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Aggregating Actions In Struts Revisited   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads