Easy Ajax using Struts 2
From Kb
Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author
Contents
See Also
Summary
Struts 2 makes it easy to implement and test an Ajax Java Web application, using standard javascript frameworks such as Prototype. This wiki article gives technical notes on how to kickstart your Ajax Java development.
Relevant Pages
Wiki
Blog
Whitepaper
Java.Net Article
Setting up Struts 2
or
Familar Parts (similar to Struts 1)
Extract from Struts.xml
<!-- just view the (advanced) search page--> <action name="displayMainPage" class="net.firstpartners.MyStrutsAction" method="prepare"> <result name="input">/WEB-INF/pages/someMainPage.jsp</result> <result name="success">/WEB-INF/pages/someMainPage.jsp</result> <result name="cancel" type="redirect">homePage.action</result> </action> <!-- just view meta information for a search--> <action name="ajaxCall" class="net.firstpartners.MyStrutsAction" method="handleAjax"> <result name="input">/WEB-INF/pages/someAjaxResponsePage.jsp</result> <result name="success">/WEB-INF/pages/someAjaxResponsePage.jsp</result> <result name="cancel" type="redirect">homePage.action</result> </action>
Extract from Struts Action Class (MyStrutsAction.java) - Action functionality
//This method will be called later by ajax public String handleAjax() { // Do something in respose to the ajax call //Forward to the (Ajax) JSP Page listed under 'success' in the Struts.xml //This page will be very simple , as all it does is replace the contents of one Div tag return SUCCESS; } public String prepare() { // Do stuff to setup the values to the dropdowns etc this.setMyDropDown(getValuesFromSomewhere()); //Forward to the JSP Page listed under 'success' in the Struts.xml return SUCCESS; }
Extract from Struts Action Class (MyStrutsAction.java) - Form functionality
Article currentArticle;
public Article getCurrentArticle() {
return currentArticle;
}
public void setCurrentArticle(Article currentArticle) {
this.currentArticle = currentArticle;
}
The New Parts (Ajax tags in Struts 2)
Main JSP Page -someMainPage.jsp
<%@ include file="/common/taglibs.jsp"%> ..... <!-- extract from the jsp page --> <!-- Show the progress of the ajax call --> <img id="indicator1" src="${pageContext.request.contextPath}/images/indicator.gif" alt="Loading Info" style="display:none"/> <!-- This is the URL of the struts action we will call in the next step --> <s:url id="ajaxCallUrl" value="ajaxCall.html"> <s:param name="someParam" value="{someValueFromTheSession}"/> </s:url> <!-- Unless we pass any other parameters, this event will fire when the page is loaded --> <!-- An Ajax call will get the information from the above (ajaxCallUrl) url and --> <!-- Replace the contents of the div tag (below) with whatever comes back from the server <s:div theme="ajax" href="%{ajaxCallUrl}" loadingText="Loading info..." indicator="indicator1"> </s:div>
JSP Page returned as result of Ajax call-someAjaxResponsePage.jsp
<%@ include file="/common/taglibs.jsp"%> <%-- This is a standard JSP Page. This uses static html, but you could use all the usual Struts tags for dynamic content --%> <p>Place your Ajax content here</p>
Event Flow
(Very similar to find / replace mechanism suggested by this article , but slicker)

