Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2, Ajax, action is only called one time

 
m pa
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using in Struts 2 with Ajax. The javascript function that creates the http request is called every 5 seconds by:

the url is mapped in struts.xml to a method in an action, I have a breakpoint in the method that is reached but it only happens once - it should be called every 5 seconds.
The method returns some text with the value of a counter that is incremented on each call but the counter value does not change - no surprise since the method's breakpoint is not reached.

I am reasonably sure javascript in the jsp is correct - I modified the function that deals with Ajax response to alter the url to a bad value and I get struts error the action request url is being handled by struts.

It seems to me that there is some "cacheing" going on in struts, pseudo code:

if action has executed return action results
else call action


Just to reiterate, the method should be called every 5 seconds but is only called once.

my struts entry:

The action class method, gets called one time:


and the jsp
 
m pa
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, seems I was right about the cacheing.

http://struts.apache.org/release/2.1.x/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html
• allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)

so fixed by:
<action name="helloAjax" class="com.mp.action.HelloWorldAction" method="helloAjax">
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
<param name="allowCaching">false</param>
</result>
</action>


oh, and the count variable needs to be a class variable:


code is working as intended.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic