How many types of action clases are there in stuts and
their uses?

Answer Posted / saranv

THERE ARE FOUR TYPES.



1)

DispatchAction: In this type of aggregation, the action
class must extend DispatchAction class as shown.

public final class CRUDDispatchAction extends
DispatchAction {

public ActionForward create(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return (mapping.findForward("success"));
}
...

and the action mapping will be as

<action path="/crudDispatchAction"
type="com.companyname.projname.CRUDDispatchAction"
name="formName" scope="request" input=" homeDef"
parameter="methodToCall">
<forward name="success" path="targetDefName"/>
</action>

in your jsp you can call this action as

<html:link action="crudDispatchAction?
methodToCall=create">Create</html:link>
...

Observe that the above class extends DispatchAction and so
you cannot use this method if your class already extends
your (some) super class (eg., the class where the session
is validated/invalidated). Here the user has to send a
query string variable (methodToCall) to set the action name
to call.


2)



ActionDispatcher: This flavor of aggregation is same as
DispatchAction except that we need not extend
ActionDispatcher, so we can use this method even if our
class extends a super class. The following code snippet
shows this scenario.


public final class CRUDActionDispatcher extends Action {

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

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return dispatcher.execute(mapping, form, request, response);
}
The DEFAULT_FLAVOR field suggests that the default
parameter is "method" if none is specified as parameter in
struts-config.xml (eg,. methodToCall).
ActionDispatcher flavor also needs methodToCall parameter
to be set (using hidden variable or a query string) as in
case of DispatchAction.




3)



LookupDispatchAction: This type of aggregation is useful in
situations where in you have multiple submit buttons in a
single form. The class must extend LookupDispatchAction.
However, the great thing about this type is that its java
script free. That means, you need not set any hidden
variables or pass query string however, you must use submit
buttons as shown.

<html:submit property="submit"><bean:message
key="button.create"/></html: submit >
<html:submit property="submit"><bean:message
key="button.read"/></html: submit >
...
The example Action class will be as follows

public class CRUDLookUpDispatchAction extends
LookupDispatchAction {

protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.create", "create");
?
return map;
}
public ActionForward create(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return (mapping.findForward("success"));
}
Observe the getKeyMethodMap() method. The submit button
names are specified in a Map and their keys comes from
MessageResources file. Struts picks up the name from this
file and redirects it to the value specified in the Map.
The calling code in jsp however has multiple submit buttons
only differing in their names.





4)



MappingDispatchAction: This aggregation extends
MappingDispatchAction class. This is the most useful type
among the four types available. But as seen in other cases,
you can use this type only when your action does not extend
any other action. The good thing about this type is that
the action mappings can differ and so need not be the same
as in all other cases. To illustrate this consider the
below mappings.

<action path="/createMappingAction"
type="com.bodhtree.CRUDMappingDispatchAction"
scope="request" input="homeDef" parameter="create">
<forward name="success" path="targetDef"/>
</action>
<action path="/readMappingAction"
type="com.bodhtree.CRUDMappingDispatchAction" name="
formName" scope="request" input="homeDef" parameter=" read">
<forward name="success" path="targetDef"/>
</action>

Notice that in the first action mapping, there is no form
bean while in the second the bean name is specified. This
means that the user has the flexibility to change the
mapping according to his needs and hence not been contained
to use a constant mapping for all the CRUD actions. Note
that in all the other types of aggregations, we must use
the same mapping for all the CRUD actions.

Is This Answer Correct ?    20 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

For a single Struts application, can we have multiple struts-config.xml files?

583


In struts.xml, what does the attribute "method" stands for in the "action" tag?

521


Explain architecture of struts2?

580


What are disadvantages of Struts?

549


What is difference between lookupdispatchaction and dispatchaction?

627






What is life cycle of an interceptor?

546


How can we integrate log4j in Struts2 application?

560


Explain struts.devmode?

563


Why we use struts in java?

512


What configurations are stored in struts configuration file ?

532


What is the purpose of '@customvalidator'?

580


What configuration files are used in struts?

525


What is the purpose of form-be tag in struct-config.xml?

558


Which design pattern is implemented by Struts2 interceptors?

550


How action mapping is configured in Struts?

566