Ajax with java, via jsp

Asked

Viewed 645 times

-1

need to pass the information of a component that is on the jsp page to a controller that is in java.

From what I understand the best way is via AJAX.

Municipal 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 id="municipality" name="municipality" information to do actions in the Controller class.

1 answer

0

If you want to call Servlet in jquery it’s 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>

Ai o in 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: https://jsfiddle.net/s2ofgga7/7/

  • Thanks Carlos But I went into his example and he doesn’t show "Alert("city: "+city);", after choosing a city. What can be ?

  • ready. I edited it for you to visualize

  • worked. But what should I put here? Function(data){ /your code/ } For example, how to call a spring Controlle ?

  • the return, recommend the json, there would be more or less if(data.retorno == 1) alert('Feito com sucesso!');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.