Struts 2 Appfuse Dojo and Ajax

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

Contents

See Also

Useful Tools

Appfuse

Appfuse allows you to create (via a Maven command) a project with Struts 2, Dojo , Acegi and Spring preconfigured.

Struts 2

Simple Ajax Setup

<s:head theme="ajax" debug="true"/>
  1. Add the line above to the Struts2 jsp within 'head' tag to ensure Web page uses ajax enabled tags - where s: is the handle to the Struts2 standard library.
    1. removing the debug=true part will turn off prototype debug messages in the browser.
  2. Add an entry in struts.xml of a action that will handle this (ajax) web request. This is normal Struts code.
  3. Add method referred to on Struts Action. This is normal Struts code (e.g. returns Success).
  4. Include a jsp page reference as success / error in your struts.xml.
    1. This is what is returned to the Ajax call.
    2. Make sure that Struts taglibs are included and that it returns good HTML.
    3. This page will be embedded in another jsp page, so it is ok (and advisable) to have no html or body tags.
  5. If you are using Appfuse (or just Spring Acegi Security) ensure that the url is added to the security.xml file - otherwise the Ajax call will just bring back the standard login page!
  6. To test the most simple possible ajax call, include a Div tag that loads content via an ajax call as soon as the page is loaded. This one has a 'loading content' message. Make sure the href maps to the one that you added to the struts.xml file
<!-- URL link to struts action-->
<s:url id="ajaxText" action="SomeAction" method="someMethodOnAction" />
 
 
<!-- Div where content will be displayed -->
<s:div theme="ajax" id="weather" href="${ajaxText}">
    loading content...
</s:div>

Ajax Events in Dojo

  • Publish Events
    • via Ajax enabled tags e.g. any tag that can have the following attribute
notifyTopics="/save"
    • via Javascript
      dojo.event.topic.publish("/refresh", "foo", "bar");
  • Listen to Events
    • Tags can listen to events
    • Javascript can register to be called
dojo.event.topic.subscribe("/refresh", function(param1, param2) {
     //this function will be called everytime "/refresh" is published
   });

Struts 2 Sample Applications


Known problems

Reference

Personal tools