Hello:
I am using jQuery to make ajax requests to a
Struts 2 Action. The first time a request is made, the execute() method runs and completes normally. However, all subsequent ajax calls to this Action result in the execute() method
not being executed and returning the same results as the first time it is run. I have to actually restart the server to get the correct results.
So the execute() method is not run after the first time yet the jQuery.ajax function is returning results anyway (equal to the first call's results). I tried both synchronous and asynchronous calls. So far this has been very bizarre for me. I hope someone can shed light on this.
Here is my jQuery:
function retrieveIndivList(context, orgid) {
if (lockIL == "free") {
lockIL == "locked";
jQuery.ajax({
data: {orgid: orgid},
url: context + '/ajax/IndividualListAjaxAction.action',
async: true,
dataType: 'text',
success: function(text) {
alert("Success! \n\n" + text);
if (agent.indexOf('msie') != -1) {
msPopulateIL(text);
}
else if (agent.indexOf('mozilla') != -1) {
nonMSPopulateIL(text);
}
lockIL = "free";
},
error: function(data) {}
});
}
}
Here is my Struts Action:
public class IndividualListAjaxAction extends BaseAction {
private Logger logger = Logger.getLogger(getClass().getName());
private Document resultDoc = null;
private
String orgid = null;
public String execute() {
String db =
(String)session.get(GallupConstants.SESSION_KEY_DB);
String securid =
(String)session.get(GallupConstants.SESSION_KEY_SECURID);
List<IndividualEditUI> indivUIList = null;
SurveyDB faithDB = SurveyDB.getInstance();
try {
Connection conn = null;
List indivOutList = null;
try {
System.err.println("orgid = " + orgid); //After first execution, this line doesn't print
conn = SurveyConnection.getConnection(db);
System.err.println("orgid = " + orgid); //After first execution, this line doesn't print
indivOutList =
faithDB.callGet_indiv_info(conn, new Long(securid),
orgid, null);
}
finally {
SurveyConnection.closeConnection(conn);
}
List indivErrorList = (List)indivOutList.get(1);
if (indivErrorList.size() == 0) {
List indivList = (List)indivOutList.get(0);
for (Object o: indivList) {
System.err.println(((IndividualInformationObject) o).getLast_name());
}
indivUIList =
SurveyUtil.extractUIListFromIndivInfoList(indivList);
Collections.sort(indivUIList);
resultDoc = createDocument(indivUIList);
}
}
catch (Exception e) {
e.printStackTrace();
logger.info(e);
}
return Action.SUCCESS;
}
private Document createDocument(List indivUIList) throws ParserConfigurationException {
//Constructs a DOM
return
doc;
}
public void setResultDoc(Document resultDoc) {
this.resultDoc = resultDoc;
}
public Document getResultDoc() {
return resultDoc;
}
public void setOrgid(String orgid) {
this.orgid = orgid;
}
public String getOrgid() {
return orgid;
}
Here is the relevant part of the result mappings:
<result-types>
<result-type name="xml" class="util.XMLResult" />
</result-types>
<action name="IndividualListAjaxAction" class="ajax.IndividualListAjaxAction">
<result type="xml">
<param name="doc">resultDoc</param>
</result>
</action>
Here is XMLResult:
public class XMLResult implements Result {
private String doc = null;
private Logger logger = Logger.getLogger(getClass().getName());
public void execute(ActionInvocation invocation) throws Exception {
ServletActionContext.getResponse().setContentType("text/plain");
PrintWriter out = ServletActionContext.getResponse().getWriter();
ValueStack valueStack = invocation.getStack();
Document docOut = (Document) valueStack.findValue(doc);
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(docOut);
trans.transform(source, result);
String xmlString = sw.toString();
out.write(xmlString);
out.close();
}
public void setDoc(String doc) {
this.doc = doc;
}
public String getDoc() {
return doc;
}
}