1

I'm using JSTL to generate a JavaScript object in a bit of inline script in a JSP, like this:

<script>
    var data = [
        <c:forEach items="${MyData}" var="Datum" varStatus="status">
        {
            foo: ${Datum.foo},
            bar: '${Datum.bar}'
        }<c:if test="${not status.last}">,</c:if>
        </c:forEach>
    ];
</script>

and Eclipse is totally unable to validate it. The HTML it generates is correct - so how do I make Eclipse stop trying to interpret/validate the JavaScript?

I've come across a number of similar questions here on SO, but none of them worked - including going to Preferences -> Validation and checking the "Suspend all validators" box!

7
  • Aside: you should consider using a proper JSON encoder to produce this sort of structure rather than trying to do it yourself. Otherwise, characters like ' in Datum.bar aren't going to get escaped to fit a JS string literal and you'll have script-injection security holes. Commented Sep 1, 2010 at 21:53
  • @bobince - You're totally right; I'll get there eventually. For now, I'm just trying to get the prototype together, and I'm actually using bar: '${fn:replace(Datum.bar, "'", "\\'")}' for exactly the problem you mentioned. The strings aren't from user input anyway... Commented Sep 1, 2010 at 21:57
  • Watch out for that last comma after bar: .... It will crash IE. Commented Sep 1, 2010 at 21:57
  • @slebetman: see the <c:if test="${not status.last}">,</c:if>? That omits the trailing comma. :) Commented Sep 1, 2010 at 22:18
  • @Bears: It omits the trailing comma for the array. But the objects within the array still has the comma. You wrote: bar: '${Datum.bar}', <-- that's a trailing comma right there Commented Sep 2, 2010 at 1:11

2 Answers 2

1

In case someone is still interested in a workaround;

Putting the code in a jsp file and including it withing the jsp file will prevent javascript validation.

<script><%@include file="data_include.jsp" %></script>

Another quick and dirty workaround:

<script>var data = [];</script>

<c:forEach items="${MyData}" var="Datum" varStatus="status">
    <c:out value="${'<script>'}" escapeXml="false"/>
        data.push({
            foo: ${Datum.foo},
            bar: '${Datum.bar}'
        });
    <c:out value="${'</script>'}" escapeXml="false"/>
</c:forEach>

Even if you do the serialization on the server side, Eclipse(Galileo) still gives validation errors for c:out and ${} within the script tag. I need to do something like the following, anyway:

<c:out value="${'<script>var data = ${jsonData};</script>'}" escapeXml="false"/>
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try to escape the code with CDATA?

<script>
  //<![CDATA[
      ...
  //]]>
</script>

With or without //

1 Comment

Much as I wish that fixed things - no such luck.

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.