Let us make a dependent list example with AjaxAnywhere.
We will take as a starting point a traditional JSP solution.
Then we will integrate it with AjaxAnywhere.
To feel and compare the page behavior with and wothout AjaxAnywhere, click on this
Online Demo
<%@ page pageEncoding="UTF-8" import="java.util.*"%>
<% // some unimportant code here
String currentLanguage = request.getParameter("language");
if (currentLanguage==null)
currentLanguage="en";
Locale[] availableLocales = Locale.getAvailableLocales();
Map languagesDisplay = new TreeMap();
Map countries = new TreeMap();
for (int i = 0; i < availableLocales.length; i++) {
Locale loc= availableLocales[i];
languagesDisplay.put(loc.getLanguage(), loc.getDisplayLanguage(Locale.ENGLISH));
if (loc.getDisplayCountry(Locale.ENGLISH).length()>0
&& loc.getLanguage().equals(currentLanguage))
countries.put(loc.getCountry(), loc.getDisplayCountry(Locale.ENGLISH));
}
%>
Without AjaxAnywhere <br>
<%@ include file="/locales_counter.jsp"%>
<form method="POST" name=main>
<select size="10" name="language" onchange="this.form.submit()">
<%@ include file="/locales_options_lang.jsp"%>
</select>
<select size="10" name="country" >
<%@ include file="/locales_options_countries.jsp.jsp"%>
</select>
</form>
<%@ page pageEncoding="UTF-8" import="java.util.*"%> <%@ page import="org.ajaxanywhere.AAUtils"%> <%@ taglib uri="http://ajaxanywhere.sourceforge.net/" prefix="aa" %> <% String currentLanguage = request.getParameter("language"); if (currentLanguage==null) currentLanguage="en"; Locale[] availableLocales = Locale.getAvailableLocales(); Map languagesDisplay = new TreeMap(); Map countries = new TreeMap(); for (int i = 0; i < availableLocales.length; i++) { Locale loc= availableLocales[i]; languagesDisplay.put(loc.getLanguage(), loc.getDisplayLanguage(Locale.ENGLISH)); if (loc.getDisplayCountry(Locale.ENGLISH).length()>0 && loc.getLanguage().equals(currentLanguage)) countries.put(loc.getCountry(), loc.getDisplayCountry(Locale.ENGLISH)); } // this code does not have to be placed inside a JSP page. It can (should) // be executed inside your Controller, if you use an MVC famework. For Struts, for example, // this code goes to an Action class. if (AAUtils.isAjaxRequest(request)){ AAUtils.addZones(request, "countriesList"); } %> <script src="aa/aa.js"></script><script>ajaxAnywhere.formName = "main";</script> With AjaxAnywhere <br> <%@ include file="/locales_counter.jsp"%> <form method="POST" name=main> <!-- Here the form does not have action attribute. This means that the form is submitted to the original URL, this JSP page in our case. However, your application may submit to a different URL. Also, you application may use GET, instead of POST, download AjaxAnywhere Demo Application for more examples. --> <select size="10" name="language" onchange="ajaxAnywhere.submitAJAX();"> <%@ include file="/locales_options_lang.jsp"%> </select> <aa:zone name="countriesList"> <select size="10" name="country" > <%@ include file="/locales_options_countries.jsp.jsp"%> </select> </aa:zone> </form>
<filter> <filter-name>AjaxAnywhere</filter-name> <filter-class>org.ajaxanywhere.AAFilter</filter-class> </filter> <filter-mapping> <filter-name>AjaxAnywhere</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
aa:zone
custom tag to mark a zone that needs to be refreshed.
ajaxAnywhere.getZonesToReload = function() { return "countriesList"; }