1

I want to do a concatenated combobox, for that I am using ajax and javascript in the view, the problem is that the controller receives the data and capture me the result but when returned kicks me error can not return Map, I'm using spring mvc.

I already have 2 libraries of json, that's not the problem I think ..

JSP

....

       jQuery(document).ready(function() { 

        var contexPath = "<%=request.getContextPath()%>";
        $('#anios').change(
            function(e){
                if(jQuery(this).val() != "-1"){
                    $('#eventos').find('option').remove().end().append(
                        '<option value="-1">--Select state--</option>');
                    e.preventDefault();
                    var val = $(this).val();
                    jQuery("#eventos").removeAttr("disabled");

                    alert(val);
                    //$('#othstate').val('').hide(); 
                    $.ajax({
                        type : "POST",
                        url : contexPath + '/eventosPublicados.html',
                        dataType : 'json',
                        data : {
                            idAnio : val
                        }, success : function(data){
                            //alert(data.lstEventos);
                            //showEventos(data.lstEventos);
                            // $('#states').html( data.lstStates ); 
                        }, error : function(e) {
                            alert('Error: '+ e);
                        }
                     });
                } else {
                    $("#eventos").attr("disabled", "disabled");
                    $('#eventos').find('option').remove().end().append(
                    '<option value="-1">--- Seleccione ---</option>');
                }
            });

            function showEventos(data) {
                for (var i = 0, len = data.length; i < len; ++i) {
                    var msajax = data[i];
                    $('#eventos').append(
                    "<option value=\"" +msajax.idEvento + "\">" + msajax.nombre + "</option>");
                }
            }

        });
    </script>

..

Controller

 @RequestMapping(value= "/eventosPublicados", headers = "Accept=application/json,application/xml")
public @ResponseBody Map<String, ? extends Object> listaEventosPublicados(@RequestParam(value="idAnio", required=true) String anio) throws Exception{
    Map<String,Object> model = new HashMap<String, Object>();

    List<Evento> eventos = this.eventoService.obtenerEventosPublicadosxAnio(Integer.parseInt(anio));
    System.out.println("evento size: " + eventos.size());

    model.put("lstEventos", eventos);

    return model;
}

If I retrieve the data from the list, which means that if the value "anio" arrives, the problem is to return.

Help me pls

1 Answer 1

1

Are you trying like this? where do you parse "eventos" to json?

 @RequestMapping(value= "/eventosPublicados", headers = "Accept=application/json,application/xml")
 @ResponseBody
 public String listaEventosPublicados (@RequestParam(value="idAnio", required=true) String anio)){
      // ..... Parsing "eventos" to json 
 }

To parse json https://code.google.com/p/google-gson/ will be useful

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.