Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Set the mapping name

This page discusses - Set the mapping name

Ads

Tutorials   
Struts 2 Tutorial and Examples 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
Ads

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? 

  1. Events can be attached to controls directly. 

  2. No need to use a separate hidden variable or a query string for mapping the method name to the actual method to call. 

  3. Actual method to call can be aliased with a pseudo name. 

What are the disadvantages? 

  1. The need to extend the class from EventDispatchAction and hence cannot extend our class from a custom base class. 

EventActionDispatcher 

How to create one? 

  1. 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? 

  1. Events can be attached to controls directly. 

  2. No need to use a separate hidden variable or a query string for mapping the method name to the actual method to call. 

  3. 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 

  1. It is assumed that you have the complete source code of this discussion with you. 
     

  2. 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. 
     

  3. 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. 
      

  4. 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. 
      

  5. 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 

 Download

Advertisements

Ads
Share on Google+Share on Google+

Set the mapping name

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Set the mapping name  

Post your Comment


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

Ads

 

Ads