我想做一个连接的组合框,因为我在视图中使用ajax和javascript,问题是控制器接收数据并捕获我的结果但是当返回踢我时错误无法返回Map,我'使用spring mvc。
我已经拥有2个json库,这不是我想的问题..
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>
...
控制器
@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;
}
如果我从列表中检索数据,这意味着如果值“anio”到达,则问题是返回。
帮帮我吧
答案 0 :(得分:1)
@RequestMapping(value= "/eventosPublicados", headers = "Accept=application/json,application/xml")
@ResponseBody
public String listaEventosPublicados (@RequestParam(value="idAnio", required=true) String anio)){
// ..... Parsing "eventos" to json
}
解析json https://code.google.com/p/google-gson/将非常有用