0

Below data is returned from Servlet to JSP.

List< List < String > > formData = new ArrayList< List< String > >();

formData has [[A, D], [, E], [B], [], [], [C]]

I am unable to loop through above list using javascript in jsp.

I tried assigning formdata to a variable and accessed it using index as below

In JSP:

var myData = "${formData}";

var i=0;

var j=0;

alert(myData[i][j]) 

alert shows the value as undefined.

Please let me know how to loop through formData list.

3 Answers 3

1

I don't think that printing the toString of a bidiminsional collection of strings from jsp in the javascript source is a good idea. That's one of the things transport/representational languages have been created for. For this reason, I would suggest to print the JSON representation of that bidimensional collection. What you need in the JavaScript source code, a bidimensional array of String is:

[["A", "D"], [, "E"], ["B"], [], [], ["C"]]

Notice the double quotes, those are important, without those, the generated javascript array is a bidimensional array of variables (A, D, E, B, C).

For example, using Gson (google json library), in Java I would create formData like this:

Gson gson = new Gson();
String toPrintInTheJSP = gson.toJson(<your Java bidimensional collection of strings here>);

In the JSP I would output:

var myData = ${formData}; //without the double quotes
var i=0, j=0;
alert(myData[i][j]) 

If you need to cycle through the collection in JSP (that means while generating the markup (I quote, "I tried assigning formdata to a variable and accessed it using index as below In JSP: [...]") ), there are different methods to do that, for example a scriptlet or JSTL.

I'm not really sure about what you have to do, the terminology you used in your question is a little bit imprecise. I think you need to cycle in JavaScript on a bidimensional array written by a JSP.

If you have to cycle through the array in JavaScript, you need a nested loop, like this:

    var myData = [["A", "D"], [, "E"], ["B"], [], [], ["C"]];
    for (var i = 0; i < myData.length; i++){
        for(var j = 0; j < myData[i].length; j++){
            alert(myData[i][j]);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the brackets around ${formdata}in your JSP:

var myData = ${formData};

var i=0;

var j=0;

alert( myData[i][j] ) ;

Your were assigning a string to myData. To loop through the array, you either need to parse that string using JSON.parse() or assign it as an array directly as shown above.

7 Comments

when i tried removing the quotes around ${formData}, alert message is not shown. In the same line, it shows a missing semicolon error
@Surabhi I put up a fiddle here. If your output is similar for ${formdata}, this should work.
I tried removing the quotes from the string and assigning it to new variable var newData = myData.substring(1, myData.length-1); but alert of newData[0][0] didnt work
@Surabhi Can you modify my fiddle in such a way, that I can see, what data arrives at the client's browser?
I was unble to edit your fiddle. I have create one here [link]jsfiddle.net/Surabhi_sn/whZrR. Basically I have introduced double quotes and converted myData to String. Please let me know how to convert String to array in Javascript. Thanks
|
0
var myData = "${formData}";

Transforms to:

var myData = "[[A, D], [, E], [B], [], [], [C]]";

In javascript, to create an array, you have to use:

var myData = [[A, D], [, E], [B], [], [], [C]];

1 Comment

when i tried removing the quotes around ${formData}, alert message is not shown for myData[0][0]. In the same line, it shows a missing semicolon error

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.