1

I have a form in a struts 2 webapp. I have the jquery plugin plugin working nicely. However on some of my pages I am reloading div's depending on user interaction and am using the following javascript to submit actions and reload divs on the page :

function doWhatever()
{    

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("mydiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","MyAction.action?id="+1);
xmlhttp.send();    
}

This all works fine, except when I reload the div with the result of the action, none of the jquery tags render correctly on the new page within the div. The regular struts s: tags do render correctly on the new page within the div On the new inner page I have included the same tags as on the main page :

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sjq" uri="/struts-jquery-tags"%>

 <html>
<head>
<sj:head />
</head>

Any help on this would be appreciated.

Struts 2 version 2.3.16.3 struts 2 jquery plugin version : 3.7.1

Spunog

Spunog
  • 309
  • 3
  • 11

1 Answers1

0

The problem is that Struts2-jquery-plugin generates scripts running on the document.ready() handler.

They works fine at page loading, but they aren't fired in case of a JSP snippet returned through an AJAX call.

Here is a working and tested solution to this problem.

For general topics about Struts2 and AJAX, you may want to read this answer too.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I'm finding that a bit hard to follow as I only use jquery through the struts tags. Could you maybe spell out what I need to do please? Do I just put in my inner page ? – Spunog Oct 06 '14 at 13:06
  • You need to 1) Enhance your up-voting rate on StackOverflow, it's the free way you can reward people the effort they put in helping others; then 2) REDEFINE the document.ready handler, as described in that answer, and 3) APPEND a as LAST LINE of each JSP snippet returned through AJAX in your application. All the work is already done, there is also a running example... just copy paste, and read each line of the answer. – Andrea Ligios Oct 06 '14 at 13:55