4

I have a JSP file containing code to populate a JavaScript array from data in the model. The JSP looks as follows:

data.addRows([
    <c:forEach items="${AuditsByTime}" var="row">
        [new Date(${row.key})<c:forEach items="${row.value}" var="c">,${c}</c:forEach>],
    </c:forEach> 
]);


NetBeans reports a syntax error on the final ]); (all 3 underlined) and reports the error:

"Expected ; but found ]".

The data itself is in the form Map<Long, int[]> in Java.

If I remove the inner forEach

data.addRows([
    <c:forEach items="${AuditsByTime}" var="row">
        [new Date(${row.key})],
     </c:forEach> 
]);

then it no longer reports an error. In both cases though the page is generated and both looks and works perfectly.

I found this: http://forums.netbeans.org/topic54289.html but it seems to be discussing a different problem as in this case I do have an error location specified in the file.

Is there a subtle problem here I've missed or have I just confused the NetBeans parser? Is there anything simple I can do to remove the error report?

If my code is broken I'll fix it, if it's the NetBeans parser then I'll report it as a bug.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Tim B
  • 40,716
  • 16
  • 83
  • 128

1 Answers1

1

It looks like you are simply confusing the parser.

That being said the generated JS will contain a dangling comma at the end of the array. You should consider using a JSTL fn:join instead of a forEach loop. Something like the following untested code may work for you (note: it also has the dangling comma problem when row.value is empty)

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
//...
data.addRows([
    <c:forEach items="${AuditsByTime}" var="row">
        [new Date(${row.key}),${fn:join(row.value,',')}],
    </c:forEach> 
]);
disrvptor
  • 1,592
  • 12
  • 23
  • Nice solution, unfortunately fn:join only seems to accept an array of strings whereas I have an array of integers (unless I missunderstood the error). I could create Strings from them when building the data structure in java but that would be quite inefficient. `javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert [I@6ecdb2d2 of type class [I to class [Ljava.lang.String;` – Tim B Jan 15 '14 at 12:45
  • That's correct, the signature is a string array `String[]`, which can't be used with Integer objects. Luckily there is an implementation here that will provide this capability. http://stackoverflow.com/questions/296398/concatenate-strings-in-jsp-el – disrvptor Jan 15 '14 at 16:31
  • That answer seems somewhat out of date, I had to write the tag library in a very different way: http://stackoverflow.com/questions/296398/concatenate-strings-in-jsp-el/21184534#21184534 it solved the problem though :) – Tim B Jan 17 '14 at 11:13