0

I find JSP works great in HTML but I often have problems using it to generate Javascript. This code isn't working, and in fact the page crashes when I try to inspect the element with Chrome's debugging tools:

var sensorData = [[
                        <c:forEach items="${tableData}" var="val" varStatus="status">
                            <c:set var="tempStr" value="X"/>

                            <c:choose>
                                <c:when test="${!fn:contains(val, tempStr)}">
                                    <fmt:formatNumber value="${val}" maxFractionDigits="0"/>
                                </c:when>
                                <c:otherwise>
                                    ${val}
                                </c:otherwise>
                            </c:choose>

                            <c:choose>
                                <c:when test="${(status.index + 1) % width == 0 && !status.last}">
                                    ],[
                                </c:when>
                                <c:when test="${status.last}">
                                    ]
                                </c:when>
                                <c:otherwise>
                                    ,
                                </c:otherwise>
                            </c:choose>


        </c:forEach>
                 ];

Here I'd like to create a 2d array with tableData, an ArrayList. Eclipse is showing errors on the first <c:when>:

Syntax error, insert "]" to complete ArrayLiteral

Can anyone see what I'm doing wrong?

7
  • I don't see anything obvious, but you don't say what those values look like. You might try it in Firefox/Firebug, or the "Tamper Data" Firefox plugin, so that you can look at exactly what that code generates and figure out what's wrong with it. Commented Jun 16, 2011 at 14:47
  • @Ravi Parekh well it's true that when the list is empty he'll end up with "[[]", but it looks OK so long as it's not empty. Commented Jun 16, 2011 at 14:48
  • 1
    I'd ignore that Eclipse error message - it's just being stupid. Commented Jun 16, 2011 at 14:48
  • 2
    Why don't you use a library like Jackson to generate JSON for you? You can stick the resultant JSON to the right-hand side of your assignment. jackson.codehaus.org Commented Jun 16, 2011 at 14:51
  • tableData is an ArrayList of Strings. I just printed it on the server and it looks good. The values are like this: [X,X,X,123.123,2345.235,X,X,455.445,X,...]. I have an error on the page that says: Uncaught ReferenceError: X is not defined (chrome) Commented Jun 16, 2011 at 14:57

1 Answer 1

1

If some of the array values are strings, then that's you're problem — you have to quote them.

        <c:otherwise>
            "${val}"
        </c:otherwise>

Now you're also going to have to worry about string values that contain quotes and other meta-characters in JavaScript's string constant token syntax. I use my own EL function for "protecting" string contents, kind-of analogous to fn:escapeXml(). Unfortunately Java is frozen in time so there's no built-in support for generating JSON, which would obviate this entire exercise. (I also have my own EL function for that, of course, because it's really not very hard for non-exotic data structures.)

Sign up to request clarification or add additional context in comments.

1 Comment

Bah you're right! Well this is why I don't like using JSP in Javascript... Anyway, I solved the problem using google-gson and it's working now, thanks everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.