• Post Reply Bookmark Topic Watch Topic
  • New Topic

Weird JSTL c:forEach problem  RSS feed

 
Wesley Baker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1834
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!