This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Problem in using DispatchAction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in using DispatchAction" Watch "Problem in using DispatchAction" New topic
Author

Problem in using DispatchAction

Mandar Velankar
Ranch Hand

Joined: Jul 31, 2006
Posts: 32
Hi,
I am using DispatchAction to call my own public method in class which extends DispatchAction, but problem is my required method is not called , instead execute() method is called. Where is problem?
Here is my code

JSP Page
....
....
<html:form action="/addshade">
<html:hidden property="method" value="mymethod" />
<table width="100%" border="0" cellpadding=3 cellspacing=1 bgcolor="#FFFFFF" class="TextContenttext" >
<tr bgcolor="#f0f0f0">
<td colspan="2"><html:errors/></td>
</tr>
....
....
</table>
</html:form>
....
....

Action mapping entry in struts-config.xml
....
....
<action-mappings>
<action path="/addshade"
type="coreservlets.production.ShadeAction"
name="shadeFormBean"
scope="session"
input="/pages/production/add_shade.jsp"
parameter="method"
>
<forward name="missing-value"
path="/pages/production/add_shade.jsp"
redirect="true"/>
<forward name="success"
path="/pages/production/saveshade.jsp"/>
</action>
</action-mappings>
....
....

Action Class

....
....
public class ShadeAction extends DispatchAction{

/** Creates a new instance of ShadeAction */
public ShadeAction() {
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
request.getSession().getServletContext().log("ShadeAction::Inside execute of ShadeAction");
....
....
}
....
....
public ActionForward mymethod(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
request.getSession().getServletContext().log("ShadeAction::Inside mymethod of ShadeAction");
return(mapping.findForward("success"));
}

}

But from logs , it shows execute() method is called.
Where is mistake? Thanks a lot.
wait for your replies.

Regards
Mandar Velankar
RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
Hi Mandar,

As far as I know you the ActionClass will run the execute() methods of the ActionClass and not any other method. In case you want to call any other method you have to call from within the execute().
Please refer to foll for more information.
http://struts.apache.org/1.2.7/api/org/apache/struts/action/Action.html


Regards,<br />Roshani
Kamesh Rao
Ranch Hand

Joined: Dec 24, 2006
Posts: 35
When using a dispatch action you need to set the parameter property in the action mapping of the struts-config.xml file with some value and when submitting such an action you have to send a request parameter with the parameter name to the one you entered in the config file and the value of this parameter should be the method which you want to call in your action.

Example:
In struts config file you will have an entry like this

< action path="/search" parameter="methodType" .... >

In your jsp you should submit as follows

http://localhost:8080/contextpath/search?methodType=someMethodName

Hope it helps
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The logic that forwards to your custom method is in the execute method of DispatchAction. If you override the execute method in your class, you override that logic, thereby disabling the ability to dispatch to another method.

In most cases when extending DispatchAction, you don't want to override the execute method, but merely write your own methods. If you do override execute, you must call super.execute(...) from that method.


Merrill
Consultant, Sima Solutions
Mandar Velankar
Ranch Hand

Joined: Jul 31, 2006
Posts: 32
Thanks Merrill,

I got the reason , because I overrided execute method.I also find the same

thing while experimenting but doesn't know reason , now i got it.

Thanks again.

Regards

Mandar Velankar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in using DispatchAction
 
Similar Threads
Struts Validation with ArrayLiist of beans
DispatchAction:seems fine, but doesn't work
optionsCollection empty
link without actionform
DispatchAction in struts 1.1