<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dependant code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 16:08:15 GMT</pubDate>
    <description>DZone Snippets: dependant code</description>
    <item>
      <title>Dependant Dropdowns Using Ajax</title>
      <link>http://snippets.dzone.com/posts/show/3705</link>
      <description>This code demonstrates using ajax for dependant combos.The sample application &lt;br /&gt;below has 2 combo boxes 1.country 2.city.&lt;br /&gt;OnChange event on the country should populate the corresponding cities in the &lt;br /&gt;city dropdown.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;ajaxscripting.js&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;var xmlHttp;&lt;br /&gt;function createXMLHttpRequest() {&lt;br /&gt;    if (window.ActiveXObject) {&lt;br /&gt;        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;    } &lt;br /&gt;    else if (window.XMLHttpRequest) {&lt;br /&gt;        xmlHttp = new XMLHttpRequest();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function doRequestUsingGETCountry(url) {&lt;br /&gt;   &lt;br /&gt;    createXMLHttpRequest();&lt;br /&gt;    queryString =   url ;&lt;br /&gt;    queryString = queryString + "&amp;selcountry="+document.forms[0].country.options[document.forms[0].country.selectedIndex].value;&lt;br /&gt;    xmlHttp.onreadystatechange = handleStateChange;&lt;br /&gt;    xmlHttp.open("GET", queryString, true);&lt;br /&gt;    xmlHttp.send(null);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function handleStateChange() {&lt;br /&gt;    if(xmlHttp.readyState == 4) {&lt;br /&gt;        if(xmlHttp.status == 200) {&lt;br /&gt;          parseResults();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;function parseResults() {&lt;br /&gt;            var responseText = document.createTextNode(xmlHttp.responseText);&lt;br /&gt;            var returnElements=xmlHttp.responseText.split("||");&lt;br /&gt;          //Process each of the elements 	&lt;br /&gt;          for ( var i=0; i&lt;returnElements.length; i++ ){&lt;br /&gt;             &lt;br /&gt;             if(returnElements[i]!="")&lt;br /&gt;            {&lt;br /&gt;             valueLabelPair = returnElements[i].split(";")&lt;br /&gt;             document.getElementById('state').options.length= returnElements.length;     &lt;br /&gt;             document.getElementById('state').options[i] = new Option(valueLabelPair[0], valueLabelPair[1]);&lt;br /&gt;            } &lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;function inc(filename)&lt;br /&gt;{&lt;br /&gt;var body = document.getElementsByTagName('body').item(0);&lt;br /&gt;script = document.createElement('script');&lt;br /&gt;script.src = filename;&lt;br /&gt;script.type = 'text/javascript';&lt;br /&gt;body.appendChild(script)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;dependantComboExample.jsp&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;%@page import="ajaxsample.SampleForm,java.util.*"%&gt;&lt;br /&gt;&lt;%@include file="taglibs.jsp"%&gt;&lt;br /&gt;&lt;%@include file="ajaxscripting.js"%&gt;&lt;br /&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;br /&gt;&lt;body onLoad="doRequestUsingGETCountry('/struts-ajax/DependantComboExample.do?');"&gt;&lt;br /&gt;&lt;br /&gt;&lt;html:form action="DependantComboExample.do" &gt;&lt;br /&gt;&lt;br /&gt;  &lt;h1&gt;&lt;/h1&gt;&lt;br /&gt;  &lt;table&gt;&lt;br /&gt;    &lt;tbody&gt;&lt;br /&gt;        &lt;tr&gt;&lt;br /&gt;            &lt;td&gt;Country&lt;/td&gt;&lt;br /&gt;              &lt;td&gt;&lt;br /&gt;                &lt;html:select property="country" name="inputForm" onchange="doRequestUsingGETCountry('/struts-ajax/DependantComboExample.do?');"&gt;&lt;br /&gt;                    &lt;html:options collection="countries" property="value" labelProperty="label"/&gt;&lt;br /&gt;                &lt;/html:select&gt;&lt;br /&gt;               &lt;/td&gt;&lt;br /&gt;               &lt;td&gt;&lt;br /&gt;               &lt;html:select property="state" name="inputForm" &gt;&lt;br /&gt;                    &lt;html:options collection="states" property="value" labelProperty="label" /&gt;&lt;br /&gt;                &lt;/html:select&gt;                  &lt;br /&gt;               &lt;/td&gt;&lt;br /&gt;        &lt;/tr&gt;&lt;br /&gt;    &lt;/tbody&gt;&lt;br /&gt;  &lt;/table&gt;&lt;br /&gt;  &lt;br /&gt;&lt;/html:form&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;GetAndPostExample&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;package ajaxsample;&lt;br /&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.net.*;&lt;br /&gt;import javax.servlet.*;&lt;br /&gt;import javax.servlet.http.*;&lt;br /&gt;import org.apache.struts.action.Action;&lt;br /&gt;import org.apache.struts.action.ActionForm;&lt;br /&gt;import org.apache.struts.action.ActionForward;&lt;br /&gt;import org.apache.struts.action.ActionMapping;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import org.apache.struts.action.ActionServlet;&lt;br /&gt;import org.apache.struts.util.LabelValueBean;&lt;br /&gt;&lt;br /&gt;public class GetAndPostExample extends Action {&lt;br /&gt;    &lt;br /&gt;    ServletContext context = null;&lt;br /&gt;    &lt;br /&gt;    public void setServlet(ActionServlet servlet)&lt;br /&gt;    {&lt;br /&gt;        context =  servlet.getServletContext();&lt;br /&gt;    }&lt;br /&gt;   public ActionForward execute(ActionMapping mapping, ActionForm inForm, &lt;br /&gt;           HttpServletRequest request, HttpServletResponse response) throws Exception {&lt;br /&gt;           response.setContentType("text/xml");&lt;br /&gt;&lt;br /&gt;          String optionSelected = request.getParameter("selcountry");        &lt;br /&gt;         //Build the response text&lt;br /&gt;         &lt;br /&gt;         SampleForm sform = (SampleForm)inForm;&lt;br /&gt;         ArrayList countries = (ArrayList)context.getAttribute("countries");&lt;br /&gt;         ArrayList states = (ArrayList)context.getAttribute("states");&lt;br /&gt;         ArrayList newStates = new ArrayList();&lt;br /&gt;         String responseText = "";&lt;br /&gt;         //sending response text in the format label:value||label;value&lt;br /&gt;         for(int i=0;i&lt;states.size();i++)&lt;br /&gt;         {&lt;br /&gt;            LabelValueBean temp = (LabelValueBean)states.get(i);&lt;br /&gt;            if(temp.getValue().startsWith(optionSelected))&lt;br /&gt;            {&lt;br /&gt;                    newStates.add(temp); &lt;br /&gt;                    responseText = temp.getLabel()+";"+temp.getValue()+"||"+responseText; &lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         if(responseText.endsWith("||"))   //chop off the ending ||&lt;br /&gt;         {&lt;br /&gt;             responseText = responseText.substring(0, responseText.lastIndexOf("||"));&lt;br /&gt;         }&lt;br /&gt;         PrintWriter out = response.getWriter();&lt;br /&gt;         out.println(responseText);&lt;br /&gt;        //Close the writer&lt;br /&gt;        out.close();&lt;br /&gt;        return(mapping.findForward("content1"));&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;CacheServlet.java&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;package ajaxsample;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;import javax.servlet.ServletConfig;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import org.apache.struts.util.LabelValueBean;&lt;br /&gt;import java.util.*;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author rupali_l&lt;br /&gt; */&lt;br /&gt;public class CacheServlet extends HttpServlet {&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;    /** Creates a new instance of CacheServlet */&lt;br /&gt;    public void init() throws ServletException {&lt;br /&gt;   &lt;br /&gt;      &lt;br /&gt;        System.out.println("In do get of CacheServlet");&lt;br /&gt;        ArrayList countries = new ArrayList(); &lt;br /&gt;        countries.add(new  LabelValueBean("India", "1" ));&lt;br /&gt;        countries.add(new  LabelValueBean("U.S","2"));&lt;br /&gt;        countries.add(new  LabelValueBean( "Australia","3"));&lt;br /&gt;        &lt;br /&gt;        ArrayList states = new ArrayList(); &lt;br /&gt;        states.add(new  LabelValueBean("Maharashtra","11"));&lt;br /&gt;        states.add(new  LabelValueBean( "Delhi","12"));&lt;br /&gt;        states.add(new  LabelValueBean("Goa","13"));&lt;br /&gt;        states.add(new  LabelValueBean( "Newjersey","21"));&lt;br /&gt;        states.add(new  LabelValueBean("NewYork","22"));&lt;br /&gt;        states.add(new  LabelValueBean( "California","23"));&lt;br /&gt;        states.add(new  LabelValueBean("Sydney","31"));&lt;br /&gt;        getServletContext().setAttribute("countries", countries);&lt;br /&gt;        getServletContext().setAttribute("states", states);&lt;br /&gt;        &lt;br /&gt;       &lt;br /&gt;    }&lt;br /&gt;        &lt;br /&gt;    &lt;br /&gt;    public void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException,&lt;br /&gt;    IOException&lt;br /&gt;    {&lt;br /&gt;        // request.getRequestDispatcher&lt;br /&gt;    }       &lt;br /&gt;            &lt;br /&gt;    &lt;br /&gt;}&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;web.xml&lt;br /&gt;------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;web-app&gt;&lt;br /&gt;  &lt;display-name&gt;Struts Blank Application&lt;/display-name&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;!-- Standard Action Servlet Configuration (with debugging) --&gt;&lt;br /&gt;  &lt;servlet&gt;&lt;br /&gt;    &lt;servlet-name&gt;CacheServlet&lt;/servlet-name&gt;&lt;br /&gt;    &lt;servlet-class&gt;ajaxsample.CacheServlet&lt;/servlet-class&gt;&lt;br /&gt;        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;&lt;br /&gt;  &lt;/servlet&gt;&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;servlet&gt;&lt;br /&gt;    &lt;servlet-name&gt;action&lt;/servlet-name&gt;&lt;br /&gt;    &lt;servlet-class&gt;org.apache.struts.action.ActionServlet&lt;/servlet-class&gt;&lt;br /&gt;    &lt;init-param&gt;&lt;br /&gt;      &lt;param-name&gt;config&lt;/param-name&gt;&lt;br /&gt;      &lt;param-value&gt;/WEB-INF/struts-config.xml&lt;/param-value&gt;&lt;br /&gt;    &lt;/init-param&gt;&lt;br /&gt;    &lt;init-param&gt;&lt;br /&gt;      &lt;param-name&gt;debug&lt;/param-name&gt;&lt;br /&gt;      &lt;param-value&gt;2&lt;/param-value&gt;&lt;br /&gt;    &lt;/init-param&gt;&lt;br /&gt;    &lt;init-param&gt;&lt;br /&gt;      &lt;param-name&gt;detail&lt;/param-name&gt;&lt;br /&gt;      &lt;param-value&gt;2&lt;/param-value&gt;&lt;br /&gt;    &lt;/init-param&gt;&lt;br /&gt;    &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;&lt;br /&gt;  &lt;/servlet&gt;&lt;br /&gt;&lt;br /&gt;  &lt;!-- Standard Action Servlet Mapping --&gt;&lt;br /&gt;  &lt;servlet-mapping&gt;&lt;br /&gt;    &lt;servlet-name&gt;action&lt;/servlet-name&gt;&lt;br /&gt;    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;&lt;br /&gt;  &lt;/servlet-mapping&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;!-- The Usual Welcome File List --&gt;&lt;br /&gt;  &lt;welcome-file-list&gt;&lt;br /&gt;    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;&lt;br /&gt;  &lt;/welcome-file-list&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;!-- Struts Tag Library Descriptors --&gt;&lt;br /&gt;  &lt;taglib&gt;&lt;br /&gt;    &lt;taglib-uri&gt;/WEB-INF/struts-bean.tld&lt;/taglib-uri&gt;&lt;br /&gt;    &lt;taglib-location&gt;/WEB-INF/struts-bean.tld&lt;/taglib-location&gt;&lt;br /&gt;  &lt;/taglib&gt;&lt;br /&gt;&lt;br /&gt;  &lt;taglib&gt;&lt;br /&gt;    &lt;taglib-uri&gt;/WEB-INF/struts-html.tld&lt;/taglib-uri&gt;&lt;br /&gt;    &lt;taglib-location&gt;/WEB-INF/struts-html.tld&lt;/taglib-location&gt;&lt;br /&gt;  &lt;/taglib&gt;&lt;br /&gt;&lt;br /&gt;  &lt;taglib&gt;&lt;br /&gt;    &lt;taglib-uri&gt;/WEB-INF/struts-logic.tld&lt;/taglib-uri&gt;&lt;br /&gt;    &lt;taglib-location&gt;/WEB-INF/struts-logic.tld&lt;/taglib-location&gt;&lt;br /&gt;  &lt;/taglib&gt;&lt;br /&gt;&lt;br /&gt;  &lt;taglib&gt;&lt;br /&gt;    &lt;taglib-uri&gt;/WEB-INF/struts-nested.tld&lt;/taglib-uri&gt;&lt;br /&gt;    &lt;taglib-location&gt;/WEB-INF/struts-nested.tld&lt;/taglib-location&gt;&lt;br /&gt;  &lt;/taglib&gt;&lt;br /&gt;&lt;br /&gt;  &lt;taglib&gt;&lt;br /&gt;    &lt;taglib-uri&gt;/WEB-INF/struts-tiles.tld&lt;/taglib-uri&gt;&lt;br /&gt;    &lt;taglib-location&gt;/WEB-INF/struts-tiles.tld&lt;/taglib-location&gt;&lt;br /&gt;  &lt;/taglib&gt;&lt;br /&gt;&lt;br /&gt;&lt;/web-app&gt;&lt;br /&gt;&lt;br /&gt;struts-config.xml&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- ================================================ &lt;br /&gt;&lt;struts-config&gt;&lt;br /&gt;&lt;!-- ================================================&lt;br /&gt;&lt;br /&gt;    &lt;form-beans&gt;&lt;br /&gt;    &lt;br /&gt;	&lt;!-- &lt;form-bean name="sampleForm" type="ajaxsample.SampleForm"/&gt;&lt;br /&gt;    --&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;!-- sample form bean descriptor for an ActionForm--&gt;&lt;br /&gt;        &lt;form-bean&lt;br /&gt;            name="inputForm"&lt;br /&gt;            type="ajaxsample.SampleForm"/&gt;&lt;br /&gt; &lt;/form-bean&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/form-beans&gt;&lt;br /&gt; &lt;action-mappings&gt;&lt;br /&gt;&lt;action path="/DependantComboExample" type="ajaxsample.GetAndPostExample" &lt;br /&gt;    	name="inputForm" scope="request" validate="false" &gt;&lt;br /&gt;    	&lt;br /&gt;    	&lt;!-- Partial Ajax Content--&gt;&lt;br /&gt;    &lt;forward name="content1" path="/dependantComboExample.jsp" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/action-mappings&gt;&lt;br /&gt;&lt;br /&gt;&lt;/struts-config&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Mar 2007 10:18:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3705</guid>
      <author>rupalilakka (rupalilakka)</author>
    </item>
  </channel>
</rss>
