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 Actions executed three times 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 "Actions executed three times" Watch "Actions executed three times" New topic
Author

Actions executed three times

Andreas Daab
Greenhorn

Joined: Dec 10, 2004
Posts: 7
In my application every action started with GET request (hmtl link) is executed three times !
How to fix this? Using saveToken(request) doesn't help and in my opinion it is a workaround and not a fix for this bug. See Prevent double submit for details.
Why struts calls the action three times?
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
How did you find that it runs 3 times?
Andreas Daab
Greenhorn

Joined: Dec 10, 2004
Posts: 7
A simple "System.out.println("Hello world");" in your execute method of your action class and checking of "tomcat/logs/stdout.log" should do it. I noticed the problem when some SQL statements were executed more than once and I got double entries.
All POST request are executed twice and all GET request are executed three times.
Struts 1.2.4
Tomcat 5.0.28
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Maybe if you provide a code of your action class...
Andreas Daab
Greenhorn

Joined: Dec 10, 2004
Posts: 7
Here is the action class:

public class MainActionHandler extends DispatchAction {

public ActionForward prepareEditComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("saveToken");
saveToken(request);
return mapping.findForward("prepareEditComment.success");
}

public ActionForward editComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("isTokenValid?");
if (isTokenValid(request,false)) {
System.out.println("resetToken");
resetToken(request);
}
return mapping.findForward("editComment.success");
}

public ActionForward doSomething(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("doSomething");
}

public String getMethodName(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String parameter) throws Exception {
return parameter;
}

}

Here is the struts config file:

<action path="/prepareEditComment"
type="MainActionHandler"
validate="false"
parameter="prepareEditComment"
scope="request">
<forward name="prepareEditComment.success" path="page1.jsp"/>
</action>

<action path="/editComment"
type="MainActionHandler"
name="editCommentForm"
parameter="editComment"
validate="false"
scope="request">
<forward name="editComment.success" path="page2.jsp"/>
</action>

<action path="/doSomething"
type="MainActionHandler"
parameter="doSomething"
validate="false"
scope="request">
<forward name="saveFolderPDF.success" path="/pages/main.jsp"/>
</action>

"prepareEditComment" prepares the form action "editComment". "editComment" is executed via POST request. I click on a link and Action "prepareEditComment" is executed via GET three times. I click on my submit button then "editComment" is executed, "isTokenValid" returns false. Then "editComment" is executed again, "isTokenValid" is true and "resetToken" is called.
"doSomething" is a simple action executed via GET request and runs three times.
If you use only POST requests you can prevent double submit with the token methods, but it is more a workaround than a solution. Why does struts execute the GET actions three times?
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Hi
I have run your action class on my Tomcat 4.0, 4.1 and 5.0 (struts 1.2.4), everything ok, it executes ONCE.
Second thing that look strange for me is your action mappings:
your get link will look smth like :
prepareEditComment.do?prepareEditComment=prepareEditComment

I'd prefer like this:

And 3rd thing (as I found): "path" attribut of forward must start with "/" (slash)
Thats all I can say..
Andreas Daab
Greenhorn

Joined: Dec 10, 2004
Posts: 7
The method "getMethodName" returns the name of the parameter "name" in the struts config file. It is easier to handle than a "DispatchAction" and the parameter "method" that has not to hand over in every action with hidden fields oder GET-parameters.
I disabled the method "getMethodName" in my action class, changed "name=firstAction" to "name=method" in the struts config file and added the parameter method in the redirect in "index.jsp" to
<logic:redirect page="/firstAction.do?method=firstAction"/>
The test output in "firstAction" is printed out two times, so this seems not to be the problem with the double or triple execution. I'll try out your idea with the "DispatchAction" and post the results.
Andreas Daab
Greenhorn

Joined: Dec 10, 2004
Posts: 7
Problem found, it was my fault and no error in struts. I use the NTLM protocol to authenticate the users and so every page is loaded three times.
Thank you for help!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Actions executed three times
 
Similar Threads
Nested Tag (in Form of Table ) :: not able to submit masterForm
Prevent double form submit
Prevent multiple submit...
In Struts How you handle multiple submit request performed once
prevent multiple submission of forms in struts