Ver Mensaje Individual
  #4 (permalink)  
Antiguo 23/04/2008, 14:45
mrtorch
 
Fecha de Ingreso: diciembre-2003
Mensajes: 31
Antigüedad: 20 años, 4 meses
Puntos: 0
Re: ajax para aplicacion web con struts

hola. yo enganché ajax para unos formularios gigantes con struts usando JSON.
y prototype.js (libreria javascript)

Existe un jar de json que debes agregar al classpath y lo encuentras en http://www.json.org/json-es.html

-----
En tu Action:
__________________
....
......
HashMap hm = new HashMap();
hm.put("executeError", "Ocurrio un error en los datos");
JSONObject json = JSONObject.fromObject(hm);
response.setHeader("X-JSON", json.toString());

return mapping.findForward(FWRD_JSON);
__________________

FWRD_JSON hace referencia a una jsp vacia "blanco.jsp"
y response.setHeader("X-JSON", json.toString()); escribe el objeto JSON en la cabecera de dicha jsp.

---
En tu javascript :

var consultaDocumento = function(nroDocumento){
new Ajax.Request('<html:rewrite page="/MyAction.do?accion=verDocumento"/>', {
parameters:{nroDoc:nroDocumento},
method:"post",
onComplete: recuperaDatos
});
}

var recuperaDatos = function(transport,json){
if(json.executeError){
alert(json.executeError); //despliega el mensaje de error

}else{
var documento = json.jdocumento;
try{ if(documento.length > 0){ //llenar formulario...
$('txtCodCli').value = documento[0].clhlCdgEmba;
$('txtSucursal').value = documento[0].clhlScrsEmba;
$('txtTipoCte').value = documento[0].tpclCdgEmba;
.....
...

}
}
__________________
el que madruga...
...ta muerto e sueño