I have a List<ProductModel> productList
in my ProductView.java class. All I want to do is something like this:
var products = ${productView.productList}
for (var i = 0; i < products.length; i++) {
console.log(products[i].productName);
}
For whatever reason (I don't know much about JavaScript, so I assume I'm doing something really wrong), I always get "Uncaught SyntaxError" among other errors.
How could I retrieve a Java list and iterate over the elements to access each product name?
For additional info, var length = '${productView.productList.size()}'
works fine (I can print the list's size from javascript) so the list is accessible I guess. I can imagine that the problem is related to the Java->Javascript mapping but I don't how to do it right.
var length = '${productView.productList.size()}'
for (var i=0; i<parseInt(length); i++) {
console.log('${productView.productList[0].productName}');
console.log('${productView.productList[1].productName}');
}
This also is showing the names for the first two products, but I can't iterate properly because I think I have no way of putting the i
variable where the literals 0
or 1
are placed inside the ${}
expression.