| Author |
Weird JSTL c:forEach problem
|
Wesley Baker
Ranch Hand
Joined: Aug 20, 2008
Posts: 40
|
|
I am trying to pass a Map key and value to two separate JavaScript arrays:
There are no Java errors, and if I use the debugger and step through it the correct values are displayed for each key and value iteration. However, JavaScript seems to be freaking out over the following line:
Because if I add an alert() to display the key, it won't ever pop up. The page finishes loading and nothing ever pops up. The value works fine though.
Am I doing something wrong trying to get the key? I've used the c:forEach before and never had this problem.
Here is the method that is supplying the Map:
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Wesley Baker wrote:However, JavaScript seems to be freaking out over the following line:
Is that actually the generated Javascript code? Because the expression ${exclusion.key} should evaluate to something, even if it's an empty string.
|
 |
Wesley Baker
Ranch Hand
Joined: Aug 20, 2008
Posts: 40
|
|
Yes, that's what actually in the code that I'm having problems with. If I do this:
I get the expected pop up telling me the values. However, if I do this:
I get nothing. No pop up. It's like the JavaScript is crashing, but I can't figure out if it's because of the String that is being passed to it, or if it's something quirky with JavaScript itself. I even tried:
But that didn't help.
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 879
|
|
I guess the question is, what do you see when you view source on the generated html page?
hint: it should NOT include any EL expressions.
I would expect alert(${exclusion.key}) to fail without quotes around the value being alerted.
ie alert('${exclusion.key}');
so if the value of exclusion.key was "foo" then alert(foo) would fail (undeclared variable) while alert('foo') would succeed.
Is your value a string that include quotes in it? or a number perhaps? alert(42) would work fine :-)
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Wesley Baker wrote:Yes, that's what actually in the code that I'm having problems with.
Could we remove the ambiguity from that statement? Is it in the JSP code, or in the Javascript code which it generates? If you have a Javascript problem then it's the latter you should be looking at, not the former.
(In case you didn't already know, you can see the code in the browser via one of the options when you right-click the page.)
|
 |
Wesley Baker
Ranch Hand
Joined: Aug 20, 2008
Posts: 40
|
|
Stefan Evans wrote:
I would expect alert(${exclusion.key}) to fail without quotes around the value being alerted.
ie alert('${exclusion.key}');
HA! You nailed it, Stefan. That has been bugging me (no pun intended) for hours. I'm so used to Java just "understanding" that when you pass a String, it comes with its own necessary quotes. Adding the single quotes outside the ${exclusion.key} did the trick.
Thanks a bunch!
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
|
Learn to look at what the browser is seeing. The JSP source is useless to debug client-side issues. A quick look at the actual generated HTML/JavaScript would pinpoint the issue immediately.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: Weird JSTL c:forEach problem
|
|
|