• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Struts 2.0.11 Action as Ajax backend

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code inside code tags to it's legible.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you clear the cache between requests?
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the caching headers--there's no reason to put the burden on your users.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I started to put an extra parameter in my Ajax calls like "time=3380141044" which is the system time. Thank you!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's another approach :)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic