DispatchAction in Struts
By: Grenfel
DispatchAction is another useful built-in Struts Action. However you cannot use it as is. You will have to extend it to provide your own implementation. An example will make things clear. Consider an online credit card application. Customers fill the credit card application online. The bank personnel get a List screen as shown in Figure below and they can act in one of four ways - Approve, Reject or Add Comment. Consequently there are three images each being a <html:link>.One way of dealing with this situation is to create three different Actions – ApproveAction, RejectAction and AddCommentAction. This is a valid approach, although not elegant since there might be duplication of code across the Actions since they are related. DispatchAction is the answer to this problem. With DispatchAction, you can combine all three Actions into one. The DispatchAction provides the implementation for the execute() method, but still is declared as abstract class. You start by sub-classing DispatchAction. Let us assume that CreditAppAction, a sub-class of DispatchAction is used to implement the above-mentioned presentation logic. It has four methods – reject(), approve() and addComment(). The CreditAppAction class definition is shown in Listing below.
You might be wondering why all the three methods take the same
four arguments – ActionMapping, ActionForm, HttpServletRequest,
HttpServletResponse. Don’t worry, you will find the answer
soon.
For a moment, look at the four URLs submitted when the bank
staff perform the three actions as mentioned before. They would look something
like this.
http://localhost:8080/bank/screen-credit-app.do?step=reject&id=2
http://localhost:8080/bank/screen-credit-app.do?step=approve&id=2
http://localhost:8080/bank/screen-credit-app.do?step=addComment&id=2
Figure: Screen Credit Applications page as seen by the bank staff.
An interesting thing to notice is that the value of the HTTP request parameter named step is same as the four method names in CreditAppAction. This is no coincidence. DispatchAction (the parent class of CreditAppAction) uses the value of the HTTP request parameter step to determine which method in CreditAppAction has to be invoked. In the execute() method, DispatchAction uses reflection to invoke the appropriate method in CreditAppAction. For this reason, the arguments on all the three methods in CreditAppAction are fixed and have to be – ActionMapping, ActionForm, HttpServletRequest, and HttpServletResponse in that order. Otherwise the method invocation by Reflection fails.
Okay, so part of the puzzle is solved. But how does
DispatchAction know to look for the HTTP request parameter specifically named step
in the URL? The simple answer is that it doesn’t. You will have to tell it
explicitly. And this is done in the ActionMapping for /screen-credit-app.do. The
ActionMapping for the URL path “/screen-credit-app.do†is declared in struts-config.xml
as shown
in Listing below.
The section highlighted in bold is what makes this Action different from the rest. The type is declared as mybank.example.list.CreditAppAction – you already knew that. Now, let us look at the second attribute in bold. This attribute, named parameter has the value “stepâ€. Notice that one of the HTTP request parameter in the four URLs is also named “stepâ€. Now, it is all coming together. DispatchAction knows what parameter to look for in the incoming URL request through this attribute named parameter in struts-config.xml. From the value of parameter attribute, it knows the method to be invoked on the subclass. Since the arguments and their order in these methods is fixed by DispatchAction, the method invocation by reflection at runtime is successful. If for any reason, the arguments on these methods are different; the method invocation fails at runtime.
//Example DispatchAction
public class CreditAppAction extends DispatchAction {
public ActionForward reject(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String id = request.getParameter(“idâ€);
// Logic to reject the application with the above id
... ... ...
mapping.findForward(“reject-successâ€);
}
public ActionForward approve(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String id = request.getParameter(“idâ€);
// Logic to approve the application with the above id
... ... ...
mapping.findForward(“approve-successâ€);
}
public ActionForward addComment(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String id = request.getParameter(“idâ€);
// Logic to view application details for the above id
... ... ...
mapping.findForward(“viewDetailsâ€);
}
...
...
}\
DispatchAction can be confusing in the beginning. But don’t worry. Follow these steps to setup the DispatchAction and familiarize yourself with the steps.
- Create a subclass of DispatchAction.
- Identify the related actions and create a method for each of the logical actions. Verify that the methods have the fixed method signature shown earlier.
- Identify the request parameter that will uniquely identify all actions.
- Define an ActionMapping for this subclass of DispatchAction and assign the previously identified request parameter as the value of the parameter attribute.
- Set your JSP so that the previously identified request parameter (Step 3) takes on DispatchAction subclass method names as its values.
//Action Mapping for the DispatchAction
<action path="/screen-credit-app"
input="/ListCreditApplications.jsp"
type="mybank.example.list.CreditAppAction"
parameter="step"
scope="request"
validate="false">
<forward name="reject-success"
path="RejectAppSuccess.jsp"
redirect="true"/>
..
</action>
Design Tip: Use DispatchAction when a set of actions is closely related and separating them into multiple Actions would result in duplication of code or usage of external helper classes to refactor the duplicated code. In the above example DispatchAction was used handle hyperlinks. DispatchAction is a good choice when there are form submissions using the regular buttons (not the image buttons). Just name all the buttons same. For instance,
<html:submit property=â€stepâ€>Update</html:submit>
<html:submit property=â€stepâ€>Delete</html:submit>
and so on. Image buttons is a different ball game. Image button usage for form submission and DispatchAction are exclusive. You have to choose one. In the above example we used the DispatchAction and used methods that has ActionForm as one of its arguments. As you learnt in the last chapter, an ActionForm always existed in conjunction with the Action. In the Listing above, all the four methods got a null ActionForm. But that did not matter since the HTTP request parameters were used directly in the Action. You can have a Form bean if there are a lot of HTTP parameters (and perhaps also require validation). The HTTP parameters can then be accessed through the Form bean.
Archived Comments
1. nice tutorial,but if provided with the complete example with out put then it would be help full for
View Tutorial By: Prasant Kumar Sahu at 2013-01-07 13:17:20
2. Following question to this topic:
- when in a dispatch action I am unable to pass a field pa
View Tutorial By: Alistair at 2012-01-14 10:59:52
3. Hi Friends,
I got a very interesting url for this topic. Try it. It contains nice exp
View Tutorial By: Rohit Kapur at 2011-07-27 20:56:57
4. Very nice to understand the concept.It also help the learner as well as experienced java people.
View Tutorial By: Hrusikesh jena at 2011-05-26 00:23:28
5. Really nice and concise article.
Thanks a lot!
View Tutorial By: vishwajeet at 2011-02-23 22:38:14
6. What then, is the purpose of the unspecified() method that is used by default? I'm assuming that it
View Tutorial By: Bryan at 2011-01-12 09:33:27
7. this tutorial is very nice but if it contains more gui figure examples,it wil very nice
View Tutorial By: JOHN.J at 2007-11-29 23:56:06
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
What is a Plug-in and how to use Java plug-ins with Struts?
Origin and Architecture of Struts
Handling multiple buttons in HTML Form in Struts