views:

20

answers:

2

Currently I am using java servlets to set up Hashmaps, lists, and range of business objects and then pass them to a jsp page via request.setAttribute("myMap", myMap);

How can I directly access this data in javascript/jquery? What is the syntax ?

At the moment I am using jstl and EL expressions to loop through data and set the values of dom elements.

<c:forEach var="entry" items="${myMap}">
<input type="hidden" class="myClass" value='<c:out value="${entry.value}" />' />
</c:forEach>

I then use javascript/jquery to access these DOM elements.

$(".myClass").eq(1).val()
$(".myClass").eq(2).val()

Is their a way I can directly access, using javascript/jQuery, the page attributes that are set in servlet without first creating dom elements ?

+1  A: 

I'd rather dump it in a javascript associative array:

<script type="text/javascript">
    var map = new Array();
    <c:forEach ... >
       map['${entry.key}'] = '${entry.value}';
    </c:forEach>
</script>
Bozho
@NimChimpsky see updated
Bozho
OK, that makes sense - wasn't sure if el expression could be accessed in javascript. That makes it a tad confusing, there is $() for jquery and ${} for el expressions ? I can therefore do something like $("#"+"${entry.value}") to select all elements with an id equal to the entry value ? I'm not saying thats a good idea btw
NimChimpsky
yes. (15chrs)..
Bozho
Great thanks! chars
NimChimpsky
Note, Mr. Chimpsky, that your EL expressions are **not** being "accessed in Javascript". The JSP parser does not have any idea what "Javascript" is, just as it doesn't know what `<table>` or `<blockquote>` are. The values are being dropped into the page text, and in this case it happens to be within a `<script>` tag, but there's nothing special about that (to the JSP system, at least).
Pointy
Also: that really should be `var map = {};` and not an Array instance. It's being used as a plain object with properties, and it's bad practice to do that with an Array.
Pointy
@Pointy @Bozho The el expressions can only be accessed directly on the jsp page, within a script tag - and not in plain old javascript files ?
NimChimpsky
@Noam that is correct, though JSP doesn't care if you use it to dynamically generate a "plain old Javascript file". In other words, you can have a JSP page at some URL that results in a pure Javascript source after processing. That's sort-of unpleasant because browsers can't cache those. Instead, you can dump "interesting" data into `<script>` blocks in the page, and then "find" those from pure static Javascript files.
Pointy
OK, I am thinking its probably easier to convert my map to json string array within the servlet (using gson), and then access that directly in javascript ?
NimChimpsky
@Noam - yes, if you do it with gson that solves many problems, including what I mentioned in my answer. You can create a Javascript data structure on the page, but keep the actual *code* in a pure JS file.
Pointy
A: 

As a note, one of the things you have to worry about when using JSP to generate Javascript is that, out of the box, there's no facility provided to "sanitize" text for that context. Specifically, in this example, if you want to put ${entry.value} into a Javascript string:

var someValue = '${entry.value}';

then you have to make sure that the value will be correctly parsed as a Javascript string. What definitely will NOT work is:

var someValue = '${fn:escapeXml(entry.value)}';

Why not? Because fn:escapeXml() is about sanitizing strings so that an XML or HTML parser won't see any metacharacters in the strings. XML and HTML have their own sensitivities, and they're just completely different from Javascript's syntax. An ampersand inside a Javascript string constant is just an ampersand, for example. However, in our example here, if ${entry.value} is the name of your Irish uncle, then upon expansion we'd have:

var someValue = 'John O'Hara';

and that's a Javascript syntax error.

To my knowledge, the JSTL/EL doesn't yet have a standardized JSON transformer. Grabbing one of the many JSON toolkits available (all with their own pluses and minuses) and wiring it up to an EL function of your own is one approach to solving this issue. Another, simpler approach is to just write your own variation of fn:escapeXml() for the purpose of "escaping" Javascript strings. It should worry about quote characters, the family of special control characters like newline and backspace, the backslash character, and Unicode characters outside the 7-bit range. With something like that, you can safely write:

var someValue = '${yourTld:escapeJS(entry.value)}';

and be confident that the generated text will be something like:

var someValue = 'John O\'Hara';
Pointy