0

Im trying to pass a Collection of items converted into String with JsonArray to my Javascript but dont work.

This is the code of the class

    InformeAmenazasAGR = manager.preparaInformeRiesgoActivos(idDimension, tipoActivo, idActivo, tipoActivoTexto, nombreActivo, recursos);       

    JSONArray JSonArray = new JSONArray();      
    JSonArray.put(InformeAmenazasAGR);
    String texto = JSonArray.toString();
    //Delete the first and last char.
    texto = texto.substring(1, texto.length()-1);   

    request.setAttribute("InformeAmenazasAGR", texto);
    return mapping.findForward( "informeActivosAGR" );

This returns one String like this:

[
["16","E.1","Errores de los usuarios","7","1128750","1015875"],
["20","E.5","Deficiencias en la organización","7","752500","526750"],
]

My JSP with Javascript (im using ExtJS and follow I tried Passing a Java string to Javascript post but dont work)

var DatosAmenazas = new String("<%request.getAttribute("InformeAmenazasAGR");%>");
var amenazaStore = Ext.create('Ext.data.Store', {
    model: 'Amenazas',
    data: DatosAmenazas     
});

What am i doing wrong? Thank you in advance

EDIT: If i put the raw String that i save in request.setAttribute("InformeAmenazasAGR", texto); it works:

var amenazaStore = Ext.create('Ext.data.Store', {
    model: 'Amenazas',
    data: [
           ["16","E.1","Errores de los usuarios","7","1128750","1015875"],
           ["20","E.5","Deficiencias en la organización","7","752500","526750"],
          ]
    });

1 Answer 1

1

I think using the Java scriptlet inside javascript is not good practice,

instead you can use the $(InformeAmenazasAGR) to set the request attribute value to a hidden element and put the hidden element anywhere inside your html <body> like this,

<input type="hidden" id="jsonData" value="${InformeAmenazasAGR}"> 

then, get the hidden element value like,

var DatosAmenazas = new String($('#jsonData').val());

if you need the request attribute InformeAmenazasAGR to be converted into json data then instead of above you can change your above line as,

var DatosAmenazas = JSON.parse($('#jsonData').val());

FYI: Java scriptlets run on server side while javascript on client side

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

2 Comments

I dont understand at all (sorry, im new with js). i added the line <input type="hidden" id="jsonData" value="${InformeAmenazasAGR}"> before the <script> and the var DatosAmenazas = new String($('#jsonData').val()); before the amenazaStore but now the page is white without load anything. i added a little description more in the question. Thank you.
@S. Moreno do not put the hidden element before the '<script>' tag but instead put it anywhere in your html '<body>' tag. Also try printing the value of variable 'DatosAmenazas' using 'alert(DatosAmenazas);' after you initialize it. See the updated answer.

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.