Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
I don't see a question mark anywhere ! – Noob UnChained Apr 21 at 16:15
it is not working is too generalized, please be more specific! – Noob UnChained Apr 21 at 16:16
${word} !!!!!!! – Noob UnChained Apr 21 at 16:17
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. – ubik Apr 21 at 16:28

1 Answer

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.

share|improve this answer
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. – user1983983 Apr 21 at 16:37
no not working for the last line it gave the error expected ; but found ] – Abdel-Rahman Shoman Apr 21 at 16:42
Edited the code! – Noob UnChained Apr 21 at 17:10
Comma at the end is fine with most browsers – Kyle Apr 21 at 17:11
Don't forget to escape the word string – Kyle Apr 21 at 17:12
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.