Struts 2 Appfuse Dojo and Ajax
From Kb
Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author
Contents |
See Also
- Main Blog - Technology and People
- Spring MVC
- Struts 2 Appfuse Dojo and Ajax
- Struts2Course Summary
- Struts 2 Web Framework
- Easy Ajax using Struts 2
- JavaScript
- Ajax - Javascript and Java - DWR , Dojo, Prototype and Scriptalicious
Useful Tools
- Visual Studio Web Developer : Free version of Visual Studio from Microsoft aimed at the web developer. http://msdn.microsoft.com/vstudio/express/downloads/default.aspx
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"/>
- 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.
- removing the debug=true part will turn off prototype debug messages in the browser.
- Add an entry in struts.xml of a action that will handle this (ajax) web request. This is normal Struts code.
- Add method referred to on Struts Action. This is normal Struts code (e.g. returns Success).
- Include a jsp page reference as success / error in your struts.xml.
- This is what is returned to the Ajax call.
- Make sure that Struts taglibs are included and that it returns good HTML.
- This page will be embedded in another jsp page, so it is ok (and advisable) to have no html or body tags.
- 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!
- 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");
- via Javascript
- 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
- Download from Apache.org (example applications): http://struts.apache.org/download.cgi?Preferred=http%3A%2F%2Fapache.mirrors.esat.net
- Ajax Sample War - Explode
- Gives samples (read the jsp) of
- Ajax Div Tag
- Ajax From Tag
- Ajax Tabbed Panel
- Ajax Tree
- Ajax Autocompletion (like google)
Known problems
Reference
- A more detailed example is available at the bottom of this (linked) page http://struts.apache.org/2.x/docs/struts-2-spring-2-jpa-ajax.html
- Ajax Theme in Struts 2: http://struts.apache.org/2.x/docs/ajax-theme.html
- More information on the Ajax Head template : http://struts.apache.org/2.x/docs/ajax-head-template.html
- Ajax Tags in Struts 2: http://struts.apache.org/2.x/docs/ajax-tags.html
- Ajax / Dojo Events System : http://struts.apache.org/2.x/docs/ajax-event-system.html
- Ajax Div Template: http://struts.apache.org/2.x/docs/ajax-theme.html

