0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Faure
  • 21
  • 3
  • 1
    Is this about javascript code in a jsp file? – Hulk Feb 11 '21 at 12:49
  • 2
    Java and Javascript are two completely separate programming languages, you'll need to explain more about your project structure and how these technologies interact, if you want useful answers. Ordinarily you'd have Java code in an application hosted on a server somewhere, exposed via something like a RESTful web service. Then your Javascript would be accessed by someone using a web browser, and the Javascript would get data from that RESTful web service. Is your setup something like that? – DPWork Feb 11 '21 at 12:53
  • Yes, sorry, it's javascript code in a jsp file. – Faure Feb 11 '21 at 12:58
  • And yes, @DPWork, that's the idea. I didn't think I had to give more details about my project structure since I'm having access to the java list from javascript. I think that if I can retrieve the size or a particular element of the list it has to be something related to my poor knowledge of javascript that I'm not being able to obtain and iterate the list itself... – Faure Feb 11 '21 at 13:06
  • ```<% code fragment %>``` .You can write all above 4 lines in java using scriptlet . Refer: https://www.tutorialspoint.com/jsp/jsp_syntax.htm – Long Nguyễn Thành Feb 11 '21 at 14:17
  • 1
    Does this answer your question? [Access Java / Servlet / JSP / JSTL / EL variables in JavaScript](https://stackoverflow.com/questions/3832792/access-java-servlet-jsp-jstl-el-variables-in-javascript) – DPWork Feb 11 '21 at 14:52
  • Thanks @LongNguyễnThành, but I think that's not what I'm looking for. I'm trying to get and iterate the java list in my javascript code because I'm interested in other future javascript work with that productName that I'm using to illustrate my problem, and I think that your solution would help me if my intention was only to show the names but does not solve my trouble accessing the list items, unless I could declare variables on that code fragment that I could use later in my JS script section. – Faure Feb 11 '21 at 14:54
  • How about this ? ``` let js_var; <% for (var i=0; i js_var = '${productView.productList[i].productName}' <%}%> ``` – Long Nguyễn Thành Feb 11 '21 at 15:17

0 Answers0