Friday, January 11, 2013

Difference between DispatchAction and MappingDispatchAction in struts.

DispatchAction action class provides a mechanism for modularizing a set of related functions into single action. This eliminates the need to create separate, independent actions for each function.
MappingDispatchAction is subclass of DispatchAction used towards the same purpose. They are almost same except the way they are designed and used. Both of them rely on use on "parameter" attribute of action in action-mappings.

The main difference is about the way "parameter" attribute is used. In case of DispatchAction, value of  "parameter" attribute is used as attribute in request scope and value of this request attribute is mapped to the function name in the action class.

Eg. if it uses parameter="operation" then, operation attribute will be searched in request scope and it's value (say add) is mapped to the function name.
On the other hand, with MappingDispatchAction value of "parameter" attribute directly mapped to the function name.

Eg. if it uses parameter="add" then add is directly mapped to function name.



Because of above difference  the URL structure is also different. For DispatchAction url contains parameter information for configured parameter(operation). so url could be like

/user.do?operation=add

And for MappingDispatchAction url is simple one which need not to have any parameters for the purpose of function name mapping. So it could be like  

/user.do

Another difference from design point of view is about configuring actions in action-mappings. In case of  DispatchAction single action tag is used.


1
2
3
4
5
6
7
8
<action path="/user" 
         name="userForm"
         type="com.nilesh.action.UserAction" 
         parameter="operation"
         scope="request"
         input="/Welcome.jsp">
    <forward name="Success" path="/user.jsp"></forward>
</action>


and it's generally mapped from a single form having different operations with different values for configured "operation" parameter.
and For MappingDispatchAction we need to use multiple actions in action-mappings so the multiple forms with different values for action attribute of the html:forms.


1
2
3
4
5
6
7
8
9
<action path="/addUser" parameter="add"  type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>
<action path="/deleteUser" parameter="delete" type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>
<action path="/updateUser" parameter="update" type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>

So the difference is just about what you need and how to use.

3 comments: