1

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>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
João Fernandes
  • 558
  • 3
  • 11
  • 29

1 Answers1

1

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:

  1. 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.

  2. 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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I understand your doubt. I'm just using AJAX because I need to send a String array (`fids`) to the Action class (`A`) and this is the only way I know to achieve that. I imagine that more suitable solutions exist, I just don't know them, so if you think there is a better way of doing this, feel free to point it out. – João Fernandes Aug 04 '15 at 09:46
  • @jaff: How about just creating a form and submit? – Aleksandr M Aug 04 '15 at 10:42
  • 1
    @jaff then look here: http://stackoverflow.com/a/15343980/1654265 I guess it's the only thing you're missing – Andrea Ligios Aug 04 '15 at 11:58
  • In fact (and as it can be seen in the code above), `fids` contains the IDs of the checkboxes that are checked, when `#finish` is pressed. I've already removed the AJAX call, added a form and turned `#finish` into a submit button, but although I have used the `status` parameter before, I just can't seem to reproduce what I was doing initially (with the AJAX call). Since comments can't handle code properly, I can edit the question and put there my efforts so far, if you want - but perhaps this is going off-topic? Anyway, I guess this is another example of an XY problem :\ – João Fernandes Aug 04 '15 at 16:09