title: Ajax with struts
body: <p>I have a problem using ajax with struts 
  I want to load dependent dropdown list using ajax. I am using struts2.</p>
  
  <p>Below is the code for the jsp.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Enter Product&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
  
      var request;
  
      function findindex1()
      {
          var temp=document.getElementById("country").value;
          if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } 
          else if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } request.onreadystatechange = showResult; 
          request.open("POST",'temp.action?index='+temp,true); 
          request.send(null);
      }
  
      function showResult(){ 
          if(request.readyState == 4){
              alert(request.responseText+"1");
          document.getElementById("text").innerHTML=request.responseText; //Just to test
          var val=&lt;%=request.getParameter("index") %&gt; -
  
          /* document.getElementById("city").flush(); */
          }
          alert(request.responseText+"1");
          }
  
      function change(){
          var temp=document.getElementById("country").value;
          location.href='loadProduct.action';
      }
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="fill()"&gt;
      &lt;s:form method="post" action="product.action" name="product" theme="simple"&gt;
          &lt;table id="table"&gt;
              &lt;tr&gt;&lt;td&gt;Country&lt;/td&gt;&lt;td&gt;&lt;s:select id="country" name="country.countryId" list="countryList" listKey="countryId"
                          listValue="name" onchange="findindex1()" &gt;&lt;/s:select&gt;&lt;/td&gt;&lt;/tr&gt; 
              &lt;tr&gt;
                  &lt;td&gt;City&lt;/td&gt;
                  &lt;td&gt;&lt;s:select id="city" name="city.id" list="cities" listKey="id"
                          listValue="name" headerKey="city.id" &gt;&lt;/s:select&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                  &lt;td&gt;Product Desc&lt;/td&gt;
                  &lt;td&gt;&lt;s:textfield name="product.description"&gt;&lt;/s:textfield&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;Product Name&lt;/td&gt;
                  &lt;td&gt;&lt;s:textfield name="product.name"&gt;&lt;/s:textfield&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;s:submit id="search" name="submit" value="Search"&gt;&lt;/s:submit&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;s:submit name="submit" value="Create"&gt;&lt;/s:submit&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
                       &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;s:label id="text" name="text"&gt;Text to change&lt;/s:label&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have checked the code and it sets the value of the List cities in the action class on changing the value of the country dropdown in the jsp. But i am unable to reflect the changes in the jsp .</p>
  
  <p>The action class function is :</p>
  
  <pre><code>public String temp()
      {
          String[] id=(String[])getParameters().get("index");
          index=Integer.parseInt(id[0]);
          cities=(ArrayList&lt;City&gt;)productCreateService.selectCities(Integer.parseInt(id[0]));
          for(Iterator&lt;Country&gt; i=countryList.iterator();i.hasNext();)
          {
              Country c = i.next();
              if(c.getCountryId()==Integer.parseInt(id[0]))
              {
                  Country c1= countryList.get(0);
                  country=c;
                  break;
              }
          }
          String out="";
          for(Iterator&lt;City&gt; i=cities.iterator();i.hasNext();)
          {
  
          }
          inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
          return SUCCESS;
      }
  </code></pre>
  
  <p>All the varibles have getter setters . I know the code is messed up . Initially i testes it for cities tag. When that didnt work i tried testing on a simple tag (here id="text") . But even that didnt work . </p>
  
  <p>The model, Services and DAO(though not here) are all correct. </p>
  
  <p>The struts.xml file is as follows .</p>
  
  
  
  <p></p>
  
  <p>
  </p>
  
  <pre><code>&lt;!-- Some or all of these can be flipped to true for debugging --&gt;
  &lt;constant name="struts.i18n.reload" value="true" /&gt;
  &lt;constant name="struts.devMode" value="true" /&gt;
  &lt;constant name="struts.configuration.xml.reload" value="false" /&gt;
  &lt;constant name="struts.custom.i18n.resources" value="global" /&gt;
  &lt;constant name="struts.action.extension" value="action" /&gt;
  
  &lt;constant name="struts.serve.static" value="true" /&gt;
  &lt;constant name="struts.serve.static.browserCache" value="false" /&gt;
  
  &lt;constant name="actionPackages" value="com.oxxo.pilot.web.action" /&gt;
  
  &lt;package name="default" extends="struts-default"  namespace = "/"&gt;
  
       &lt;global-results&gt;
          &lt;result name="custom_error"&gt;/jsp/exception.jsp&lt;/result&gt;
          &lt;result name="input"&gt;/jsp/fail.jsp&lt;/result&gt;
      &lt;/global-results&gt;
  
      &lt;global-exception-mappings&gt;
          &lt;exception-mapping exception="java.lang.Exception"
          result="custom_error" /&gt;
  </code></pre>
  
  <p> </p>
  
  <pre><code>     &lt;action name="temp" class="ProductAction" method="temp"&gt;
           &lt;result type="stream"&gt;
              &lt;param name="contentType"&gt;text/html&lt;/param&gt;
              &lt;param name="inputName"&gt;inputStream&lt;/param&gt;
          &lt;/result&gt;
      &lt;/action&gt;
  &lt;/package&gt;
  </code></pre>
  
  <p></p>
  
  <p>Could somebody please tell me what am i doing wrong. If you need any part of the code please let me know . Thanks .</p>