简体   繁体   中英

Struts2 Action called twice

I am calling struts action like

<a href="someactionLink.action">Do action</a>

In Struts.xml

<action name="*Link" method="{1}" class="com.shashi.getalldata.LinkAction">

The problem is the action called twice. Once when i click the href and second time while page loading. Do any one have any idea on what's the case here?

Do you have anything on the page that could result in a subsequent request? Eg favicon/stylesheet/image/script. By favicon, I mean lack of, but the browser might be looking (most browsers only look in one place for them).

Check the access logs for a subsequent request.

Take a look at this link. It helped me figure out my issue with a double submit. It had nothing to do with my code, it was completely YSlow plug-in for FireFox 3.X and 4.X.

Double submit solution

Solution to my double submit issue.

first off: I was doing a form submit using javascript. Like:

<a href="#" onclick="save();">
      <input type="image" src="images/save.gif" alt="Save" title="Save" /></a>

where save() was:

function save() {
     ...
     document.form_name.action = "Struts2_action.action";
     document.forms[1].submit(); 
   }

Well I was NOT adding "return false;" after save() to do the following:

Return false follow three steps

  • First It stops the browsers default behaviour.
  • It prevents the event from propagating the DOM
  • Stops callback execution and returns immediately when called.

So, my anchor became:

<a href="#" onclick="save();return false;">
          <input type="image" src="images/save.gif" alt="Save" title="Save" /></a>

and the double submit problem was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM