aspose file tools*
The moose likes Struts and the fly likes Using Struts 2.0.11 Action as Ajax backend Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Struts 2.0.11 Action as Ajax backend" Watch "Using Struts 2.0.11 Action as Ajax backend" New topic
Author

Using Struts 2.0.11 Action as Ajax backend

John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
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;
}
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please post the code inside code tags to it's legible.
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
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:



Here is my Struts Action:



Here is the relevant part of the result mappings:



Here is XMLResult:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What happens if you clear the cache between requests?
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
When I clear the cache between calls I get the correct results. I'll have to ask my users not to cache. This might become an imposition but that's life. Thank you!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Set the caching headers--there's no reason to put the burden on your users.
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
I started to put an extra parameter in my Ajax calls like "time=3380141044" which is the system time. Thank you!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's another approach :)
 
jQuery in Action, 2nd edition
 
subject: Using Struts 2.0.11 Action as Ajax backend