0

I am sending an arrayList from a java file to a .jsp file

In order to receive that array I used the following code

var words = 
        [
            <c:forEach begin="0" items="${requestScope.WordList}" var = "word">
                word,
             </c:forEach>
        ];

however it is not working .. any Idea of how to do it ?

4
  • I don't see a question mark anywhere ! Commented Apr 21, 2013 at 16:15
  • it is not working is too generalized, please be more specific! Commented Apr 21, 2013 at 16:16
  • 1
    I don't think it is good practice to build JSON arrays this way. Just use a 3rd party library to do that. This kind of approach can create a lot of problems. Commented Apr 21, 2013 at 16:28
  • possible duplicate of Converting a Java ArrayList of strings to a JavaScript array Commented Mar 22, 2014 at 15:30

1 Answer 1

1

Possible fix (Bad fix):

var words = 
    [
        <c:forEach items="${requestScope.WordList}" var="word" 
         varStatus="status">
          "${word}"<c:if test="${not status.last}">,</c:if>
        </c:forEach>
    ];

OR

Convert the Java ArrayList to JSON String, and use JSON.parse() to get Javascript object.

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

4 Comments

As the type of word probably is string you will have to put quotes around ${word}. I.e. put "${word}", in the c:forEach. A secound problem would be, that you will have a comma at the end of the array definition.
no not working for the last line it gave the error expected ; but found ]
Comma at the end is fine with most browsers
Don't forget to escape the word string

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.