ae asse
ae asse

Reputation: 38

Javascript + JSP

I have a controller and a JSP File.

In my controller I have :

request.setAttribute("AListOfClient", AListOfClient );// AListOfObject is a List

In my jsp File :

<input type="hidden" id="AListOfClient" name="AListOfClient" value="${AListOfClient}" />

...

<script type="text/javascript">
var item = ${(AListeOfClient[0]).name}; // no problem with this
</script>

But I want to do a for :

for (i=0; i < ${length}; i++) {
var item = ${(AListeOfClient[i]).name}; // ERROR
var item = ${(AListeOfClient[0]).name}; // No Problem
});

Someone can help me??

For the solution :

var jsAListOfClient= [
<c:forEach items="${AListOfClient}" var="client">
"${client.name}",
</c:forEach>
                    ];

Eclipse said that "Syntax error on token ""${client.name}"", delete this token"

If I remplace "${client.name}" by " ", Eclipse says the same thing...

Upvotes: 0

Views: 271

Answers (2)

Darshan Mehta
Darshan Mehta

Reputation: 30809

You can't store the list as a hidden variable in jsp file. Better you iterate that list using JSTL and create hidden variables.

Why do you want to store the list on jsp page?

Anyways, here is the alternate approach:

Iterate the list using JSTL, create the hidden variables, give them proper ids. And in javascript, iterate over these hidden variables and you will get the required values. Javascript is client side whilst the list you are attaching in request object is server side, hence it can't be accessed.

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47280

IN your javascript do this, simply create js array and poplutae with jstl.

var jsAListOfClient= [
        <c:forEach items="${AListOfClient}" var="client">
        "${client.name}",
        </c:forEach>
    ];

then you cna reference your jsList how you want. But I agree with others just refactor and use jstl.

Upvotes: 1

Related Questions