Reputation: 558
I have been dealing with this kind of situations here and there. In my Struts 2 application, I'm using an AJAX call to send a String array to an Action class. What I'm trying to do is something like this: page_1.jsp
-> Action A
-> Action B
-> Action C
. However, what is actually happening is page_1.jsp
-> Action A
-> Action B
-> Action C
-> Action C
, i.e. the last Action class is being called twice.
page_1.jsp:
<button class="ink-button double-vertical-space all-25 dynamicButton"
id="finish" disabled> Finish </button>
[...]
$('#finish').click(function(event)
{
var fids = $('input:checkbox').filter(':checked').map(function ()
{
return this.id;
}).get();
$.ajax
({
method: "POST",
url: "A.action",
data: { fids : fids },
traditional: true,
success:
function()
{
// I know that the problem is here.
// Currently I have it like this, so it's understandable
// why action C is getting called 2 times,
// I just don't know how to fix it.
window.location = "C.action";
}
});
});
struts.xml:
<action name="A" class="action.A" method="execute">
<result name="success" type="redirectAction"> B </result>
</action>
<action name="B" class="action.B" method="execute">
<result name="success" type="redirectAction"> C </result>
</action>
<action name="C" class="action.C" method="execute">
<result name="success"> /page_2.jsp </result>
</action>
Upvotes: 1
Views: 1214
Reputation: 50231
You are doing strange things. If you want to perform some operations and go to a new page, then why are you using AJAX ? I guess not to let the user work meanwhile:
user runs the AJAX call, then starts working on something else, and in the middle of the unsaved work... PUFFF! The page changes because the AJAX response is come back.
That would be weird, uber-annoying and a clear violation of POLA.
BTW, if you still want to do it AJAX way for some reason I can't imagine right now, then you can do it in two ways:
If you want to draw page_2.jsp, as it seems, then return a dispatcher
result (or stream
, or json
, or whatever is NOT a redirect
nor redirectAction
) from your Action.B, then your window.location = "C.action";
will call Action.C the right (non-AJAX) way.
If instead you want (but it seems NOT your case) just the URL to reflect the new action, use history.pushState()
or history.replaceState()
instead of window.location()
. They won't trigger any request, and the URL will be the new one.
Upvotes: 1