Ajax with java, via jsp

-1

I need to pass information from a component that is on the jsp page to a controler that is in java.

So I understand the best way is via AJAX.

Municipality component

<label for="municipio"><font style="color: red">*</font> Munic&iacute;pios</label>
<select class="chosen-select form-control" id="municipio" name="municipio" 
            data-placeholder="Selecione um munic&iacute;pio">
<option value="-1"></option>
<c:forEach var="entidade" items="${listaEntidades}">
<option value="${entidade.id}">${entidade.municpio.codigoNome}</option>
</c:forEach>
</select>

The information is showing correctly on the screen.

I need this information from the component id="municipio" name="municipio", to do actions in the Controller class.

    
asked by anonymous 16.03.2017 / 20:22

1 answer

0

If it is to call the servlet in jquery it is simple ó

<label for="municipio"><font style="color: red">*</font> Munic&iacute;pios</label>
<select class="chosen-select form-control" id="municipio" name="municipio" 
            data-placeholder="Selecione um munic&iacute;pio">
         <option value="">Selecione</option>

         <option value="M">Manaus</option>
         <option value="S">São Paulo</option>
         <option value="R">Rio de Janeiro</option>

</select>

Then your javascript would look like this

 $('.chosen-select').on('change', function () {
        var cidade = document.getElementById('municipio').value;
        alert('Codigo da cidade: '+cidade);
        $.post("servlet",
            {
                'cidade': cidade,
                'acao': "B"
            },
            function(data){
                         /*seu codigo*/
            });
    });

Note that in the url I just called the mapped servlet respectively

Example: link

    
16.03.2017 / 21:42