开发者

Trouble making a Javascript array using JSP

开发者 https://www.devze.com 2023-03-13 06:06 出处:网络
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 debugg

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>
开发者_Python百科                                <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?


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号