Ajax com 3 ComboBox

Estou tentando fazer um ajax com 3 combobox…mas n estou conseguindo, o segundo nao esta carregando d acordo c o q eu escolho no primeiro…os meus arquivos onde o tenho o comando
ajax.open(“POST”, “<%=base%>/ajax/secoes.jsp”, true); e ajax.open(“POST”, “<%=base%>/ajax/subsecoes.jsp”, true);
estao funcionando perfeitamente, pois quando digito esses arquivos na url passando os parametros correspondentes, ele carrega o xml com os resultados certinhos…entao creio q onde estou errando deve ser no javascript…

javascript.js

function dados&#40;&#41;
&#123;
		 try &#123;
         ajax = new ActiveXObject&#40;&quot;Microsoft.XMLHTTP&quot;&#41;;
      &#125;
      catch&#40;e&#41; &#123;
         try &#123;
            ajax = new ActiveXObject&#40;&quot;Msxml2.XMLHTTP&quot;&#41;;
         &#125;
	     catch&#40;ex&#41; &#123;
            try &#123;
               ajax = new XMLHttpRequest&#40;&#41;;
            &#125;
	        catch&#40;exc&#41; &#123;
               alert&#40;&quot;Esse browser não tem recursos para uso do Ajax&quot;&#41;;
               ajax = null;
            &#125;
         &#125;
      &#125;
	  if&#40;ajax&#41; &#123;
	comboSecoes&#40;ajax&#41;;
	comboSubSecoes&#40;ajax&#41;;
	&#125;
&#125;

function comboSecoes&#40;ajax&#41;
&#123;
     //deixa apenas o elemento 1 no option, os outros são excluídos
	 document.frmAjax.codSec.options.length = 1;
	 idOpcao  = document.getElementById&#40;&quot;opcoes&quot;&#41;;
     ajax.open&#40;&quot;POST&quot;, &quot;secoes.jsp&quot;, true&#41;;
	 ajax.setRequestHeader&#40;&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;&#41;;

	 ajax.onreadystatechange = function&#40;&#41; &#123;
     //enquanto estiver processando...emite a msg de carregando
     if&#40;ajax.readyState == 1&#41; &#123;
		idOpcao.innerHTML = &quot;Carregando seções...!&quot;;
	 &#125;
	 //após ser processado - chama função processXML que vai varrer os dados
     if&#40;ajax.readyState == 4 &#41; &#123;
		if&#40;ajax.responseXML&#41; &#123;
			processXMLSecao&#40;ajax.responseXML&#41;;
		&#125;
		else &#123;
			//caso não seja um arquivo XML emite a mensagem abaixo
			idOpcao.innerHTML = &quot;Selecione a categoria&quot;;
		&#125;
     &#125;
	 //passa o código da categoria escolhida
     var params = &quot;codCat=&quot;+document.frmAjax.codCat.value;
     ajax.send&#40;params&#41;;
&#125;
&#125;

function dadosSecoes&#40;&#41;
&#123;
	   var arr = document.frmAjax.codSec.value;
	   comboSubSecoes&#40;&#41;;
&#125;

function comboSubSecoes&#40;ajax&#41;
&#123;
     //deixa apenas o elemento 1 no option, os outros são excluídos
	 document.frmAjax.codSubSec.options.length = 1;

	 idOpcao  = document.getElementById&#40;&quot;opcoes&quot;&#41;;
     ajax.open&#40;&quot;POST&quot;, &quot;subsecoes.jsp&quot;, true&#41;;
	 ajax.setRequestHeader&#40;&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;&#41;;

	 ajax.onreadystatechange = function&#40;&#41; &#123;
     //enquanto estiver processando...emite a msg de carregando
     if&#40;ajax.readyState == 1&#41; &#123;
		idOpcao.innerHTML = &quot;Carregando subseções...!&quot;;
	 &#125;
	 //após ser processado - chama função processXML que vai varrer os dados
     if&#40;ajax.readyState == 4 &#41; &#123;
		if&#40;ajax.responseXML&#41; &#123;
			processXMLSubSecao&#40;ajax.responseXML&#41;;
		&#125;
		else &#123;
			//caso não seja um arquivo XML emite a mensagem abaixo
			idOpcao.innerHTML = &quot;Selecione a seção&quot;;
		&#125;
     &#125;
	 //passa o código da categoria escolhida
     var params = &quot;codCat=&quot;+document.frmAjax.codCat.value + &quot;&amp;codSec=&quot;+document.frmAjax.codSec.value;
     ajax.send&#40;params&#41;;
&#125;
&#125;

   function processXMLSecao&#40;obj&#41;&#123;
      //pega a tag secao
      var dataArray   = obj.getElementsByTagName&#40;&quot;secao&quot;&#41;;

	  //total de elementos contidos na tag secao
	  if&#40;dataArray.length &gt; 0&#41; &#123;
	     //percorre o arquivo XML paara extrair os dados
         for&#40;var i = 0 ; i &lt; dataArray.length ; i++&#41; &#123;
            var item = dataArray&#91;i&#93;;
			//contéudo dos campos no arquivo XML
			var codigo =  item.getElementsByTagName&#40;&quot;codigo&quot;&#41;&#91;0&#93;.firstChild.nodeValue;
			var descricao = item.getElementsByTagName&#40;&quot;descricao&quot;&#41;&#91;0&#93;.firstChild.nodeValue;

	        idOpcao.innerHTML = &quot;&quot;; //Selecione a seção

			//cria um novo option dinamicamente
			var novo = document.createElement&#40;&quot;option&quot;&#41;;
			    //atribui um ID a esse elemento
			    novo.setAttribute&#40;&quot;id&quot;, &quot;opcoes&quot;&#41;;
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.frmAjax.codSec.options.add&#40;novo&#41;;
		 &#125;
	  &#125;
	  else &#123;
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = &quot;Selecione a categoria&quot;;
	  &#125;
   &#125;

function processXMLSubSecao&#40;obj&#41;&#123;
      //pega a tag secao
      var dataArray   = obj.getElementsByTagName&#40;&quot;subsecao&quot;&#41;;

	  //total de elementos contidos na tag secao
	  if&#40;dataArray.length &gt; 0&#41; &#123;
	     //percorre o arquivo XML paara extrair os dados
         for&#40;var i = 0 ; i &lt; dataArray.length ; i++&#41; &#123;
            var item = dataArray&#91;i&#93;;
			//contéudo dos campos no arquivo XML
			var codigo =  item.getElementsByTagName&#40;&quot;codigo&quot;&#41;&#91;0&#93;.firstChild.nodeValue;
			var descricao = item.getElementsByTagName&#40;&quot;descricao&quot;&#41;&#91;0&#93;.firstChild.nodeValue;

	        idOpcao.innerHTML = &quot;&quot;; //Selecione a seção

			//cria um novo option dinamicamente
			var novo = document.createElement&#40;&quot;option&quot;&#41;;
			    //atribui um ID a esse elemento
			    novo.setAttribute&#40;&quot;id&quot;, &quot;opcoes&quot;&#41;;
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.frmAjax.codSubSec.options.add&#40;novo&#41;;
		 &#125;
	  &#125;
	  else &#123;
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = &quot;Selecione a seção&quot;;
	  &#125;
   &#125;

arquivo.jsp

&lt;form name=&quot;frmAjax&quot;&gt;
&lt;select name=&quot;codCat&quot; onChange=&quot;dados&#40;&#41;;&quot;&gt;
&lt;option value=&quot;0&quot;&gt;Selecione a categoria&lt;/option&gt;
&lt;option value=&quot;1&quot;&gt;Categoria1&lt;/option&gt;
&lt;option value=&quot;2&quot;&gt;Categoria2&lt;/option&gt;
&lt;option value=&quot;3&quot;&gt;Categoria3&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;select name=&quot;codSec&quot; onChange=&quot;dadosSecoes&#40;this.value&#41;;&quot;&gt;
   &lt;option id=&quot;opcoes&quot; value=&quot;0&quot;&gt;Selecione a categoria&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;select name=&quot;codSubSec&quot;&gt;
   &lt;option id=&quot;opcoes&quot; value=&quot;0&quot;&gt;Selecione a seção&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

Isso para o trabalho ou para aprendizado?!

Pesquise sobre o DWR. :wink:

Isso é para o meu aprendizado!!!Baixei o dwr para a minha aplicacao e ja fiz o seguinte…

Classe.java

package com.dwr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.form.CategoriaForm;
import com.form.SecaoForm;
import com.util.FactoryBO;

public class SecaoAjax &#123;
	
	public Collection getCategorias&#40;&#41;&#123;
		Collection cats = new ArrayList&#40;&#41;;
		Collection categorias = FactoryBO.getCategoriaBO&#40;&#41;.listar&#40;&#41;;
		Iterator it = categorias.iterator&#40;&#41;;
		while&#40;it.hasNext&#40;&#41;&#41;&#123;
			CategoriaForm cf = &#40;CategoriaForm&#41; it.next&#40;&#41;;
			cats.add&#40;cf.getNome&#40;&#41;&#41;;
		&#125;
		return cats;
	&#125;
	
	public Collection getSecoes&#40;int cat&#41;&#123;
		Collection secs = new ArrayList&#40;&#41;;
		Collection secoes = FactoryBO.getSecaoBO&#40;&#41;.listar&#40;cat&#41;;
		Iterator it = secoes.iterator&#40;&#41;;
		while&#40;it.hasNext&#40;&#41;&#41;&#123;
			SecaoForm sf = &#40;SecaoForm&#41; it.next&#40;&#41;;
			secs.add&#40;sf.getNome&#40;&#41;&#41;;
		&#125;
		return secs;
	&#125;
	
&#125;

dwr.xml

&lt;create creator=&quot;new&quot; javascript=&quot;SecaoAjax&quot;&gt;
   &lt;param name=&quot;class&quot; value=&quot;com.dwr.SecaoAjax&quot;/&gt;
&lt;/create&gt;
&lt;convert converter=&quot;form&quot; match=&quot;com.form.CategoriaForm&quot;/&gt;
&lt;convert converter=&quot;form&quot; match=&quot;com.form.SecaoForm&quot;/&gt;

Arquivo javascript gerado .js


// Provide a default path to dwr.engine
if &#40;dwr == null&#41; var dwr = &#123;&#125;;
if &#40;dwr.engine == null&#41; dwr.engine = &#123;&#125;;
if &#40;DWREngine == null&#41; var DWREngine = dwr.engine;

if &#40;SecaoAjax == null&#41; var SecaoAjax = &#123;&#125;;
SecaoAjax._path = '/teste/dwr';
SecaoAjax.getSecoes = function&#40;p0, callback&#41; &#123;
  dwr.engine._execute&#40;SecaoAjax._path, 'SecaoAjax', 'getSecoes', p0, callback&#41;;
&#125;
SecaoAjax.getCategorias = function&#40;callback&#41; &#123;
  dwr.engine._execute&#40;SecaoAjax._path, 'SecaoAjax', 'getCategorias', callback&#41;;
&#125;

minha jsp

&lt;form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;&gt;
  &lt;p&gt;
    &lt;select&gt;
	 &lt;option value=&quot;1&quot;&gt;Brinquedos&lt;/option&gt;
	 &lt;option value=&quot;2&quot;&gt;Cama, Mesa &amp; Banho&lt;/option&gt;
	 &lt;option value=&quot;3&quot;&gt;Moda&lt;/option&gt;
    &lt;/select&gt;
&lt;/p&gt;
  &lt;p&gt;
    &lt;select&gt;&lt;/select&gt;
&lt;/p&gt;
&lt;/form&gt;

Agora como faço para popular o meu segundo combobox? qual a funcao do javascript q tenho q chamar? Parei aki e nao sei mais o q fazer…Alguem me ajuda?

Excelentes artigos sobre o DWR: http://www.handersonfrota.com.br/?page_id=12

O carneiro já te indicou meu site, mas vou te indicar o artido que fiz exatamente isso que vc está querendo.

http://www.handersonfrota.com.br/?page_id=12

6.Combos Aninhadas(Chamadas Aninhadas ao DWR)

Nesse artigo eu explico como fazer isso passo a passo. ;D

Em breve vou colocar a versão em PDF, sempre coloco. Tem tbm as vídeo aulas desses artigo caso seja assinante da Java Magazine ;D o link tbm está no meu site.

Espero ter ajudado.