Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL and quotes (HFSJ pg373)

 
Paul Kemp
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just reading about EL for the first time.

On page 373 of HFSJ there is an example of servlet code:

String[] favoriteMusic = {“Zero 7”, “Tahiti 80”, “BT”, “Frou Frou”};
request.setAttribute(“musicList”, favoriteMusic);

which can be accessed in a jsp by

${musicList[0]}
${musicList[“1”]}

If I put

${musicList[“1”]}

into the jsp, Eclipse 3.4 gives me an error message "Multiple annotations at this line". If I put

${musicList[1]}

it works fine, and outputs Tahiti 80 to the jsp. I can't use double or single quotes. What am I doing wrong?
 
Paul Kemp
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be just an Eclipse thing.

If I put

in the servlet

java.util.Map musicMap = new java.util.HashMap();
musicMap.put("Ambient", "Zero 7");
musicMap.put("Surf", "Tahiti 80");
musicMap.put("DJ", "BT");
musicMap.put("Indie", "Travis");
request.setAttribute("musicMap", musicMap);

and in the jsp

Ambient is: ${musicMap["Ambient"]}
Ambient is: ${musicMap[Ambient]}

the top one shows an error in the jsp, and the bottom one doesn't; but when I run it, the top one outputs "Zero 7" correctly and the bottom one outputs nothing, which is the correct behaviour as I understand it.
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul

the top one shows an error in the jsp, and the bottom one doesn't; but when I run it, the top one outputs "Zero 7" correctly and the bottom one outputs nothing, which is the correct behaviour as I understand it.


second time you run this code with which tool??

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the top one outputs "Zero 7" correctly and the bottom one outputs nothing, which is the correct behaviour as I understand it.


yes paul you are right.
1. if you put quote around the elemant in the [] it look for the Map key or Bean property. As in the first case Ambient is: ${musicMap["Ambient"]} it did print the value of the Map key "Ambient" that is Zero 7.

2. But if you do not quote the element the EL search for the attribute by this name and if its find one it will replace with its value. in second case Ambient is: ${musicMap[Ambient]} it look for the Ambient attribute but it dosen't find anythin. so it output nothing.

I hope this will help
 
Paul Kemp
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm running it from within Eclipse Ganymede 3.4 with Tomcat container in all cases. The results appear to be rendering correctly, as per what is said in HFSJ, it's just that Eclipse is giving me error messages (not warnings) in my jsp when I type in the text above. At run time it seems to work though, which is the weird thing.

I guess we're not supposed to use IDEs for SCWCD but I just wondered if anyone else had encountered this behaviour or am I making a basic error?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17646
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganymede has a defective XML editor. When you reformat xhtml, it frequently drops quotes form the xmlns attributes, and even when it doesn't, if often flags quoted items incorrectly as having unbalanced quotes. You can tell when you've encountered this bug if you add another quote to the offending item and then delete it and the bogus error indication goes away.

I'm fairly sure that the JSP editor shares some of this defective functionality.

In strict XML, it's not legal to use XML reserved characters in contexts that might confuse the parser. Two of the biggest offenders being use of the less-than/greater-than characters and/or double-quotes within attributes. Which is a primary reason why EL allows alternative, XML-friendly notations.

Basic (non-XML) JSP is more forgiving, but like I said, I think the JSP editor is using XML editor code. It's probably a good idea to use XML-friendly notations even in the JSP, however. Builds good habits.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic