• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type Issue with EL

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class called "user" and am trying to access an array of these guys that is set up in a servlet with EL, and it appears that EL thinks that it is a string.

Here is the servlet code where it is set up:



The user list (with a length of one for testing purposes) seems OK in the servlet.

The getters user.getUserKey(), user.getUserID(), etc. are all defined, and set up as per the following example:



and here is the point in the JSP where I'm trying to access it (the <option> tag and it's body):



The error I get is:



It *seems* that tomcat thinks it's a string, instead of a user. This is the first time I've use EL or JSTL extensively, but thought type coercion would fix it.

Any insights?

TIA
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<c:forEach items="userlist" var="u">


Try:

<c:forEach items="${userlist}" var="u">
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, that was it! Thanks a lot!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done that more times than I care to admit
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing is, do you understand the difference?
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a good chance I don't understand the difference. The syntax I was using was similar to the way one would access a bean, attribute or a name/value pair. The syntax suggested by Ben that worked was EL syntax. I suppose I must confess it is not clear to me when one uses one versus the other, why in this context the access is as an EL variable when I set it using HttpRequest.setAttribute().

The answer may also explain another problem I was just going to post. I don't have a clear understanding at all as to when the various variable go in and out of scope,and exactly how they're accessed under various circumstances.

Please continue...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not confuse the JSTL tags with the classic actions like <jsp:getProperty>. They have little in common.

You should have the JSTL Specification open on your desk. The descriptions of the JSTL action in that document are essential.

In

<c:forEach items="userlist" var="listOfUsers">

the items attribute is the string containing the characters u s e r l i s and t.

In

<c:forEach items="${userlist}" var="listOfUsers">

the EL ${} notation makes its content an EL expression; in this case one that references a scoped variable named userlist.
[ September 22, 2006: Message edited by: Bear Bibeault ]
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh! I think I get it. What I really had was:



so when I used


later in the jsp, it interpreted the

as the first element of the attribute

and thought I was trying to use a member of the string class, hence that string-related error.

Right?

Thanks a million!

p.s., sorry about all the code tags; the reply system won't let me use the letter

by itself.
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! That was a hassle!

How did you do:



in your post? This thing wouldn't let me use a "you" by itself.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With
items="userlist"

you've got a string literal in the items attribute.
With ${userList} you've got a variable that points the scoped variable named "userList".

Your page blew up when you tried to dereference properties that didn't exist in the string literal.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. In the first example, you passed it a string literal, so it was iterating over it rather than your intended scoped variable.

And about "u"... that's because too many people think that u is an acceptable form of the word "you". It's not.
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. I understand perfectly that "u" doesn't equal "you" and only use it under extreme circumstances (like trying to text message and drive at the same time;-). Those of us in the civilized world also capitalize and punctuate, so I actually strongly agree with the policy.

But when you need it, like you did, how do you get the message board to take it? Is there an escape sequence?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&#117;


[ September 22, 2006: Message edited by: Ben Souther ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
&#117;



Knowledge will set you free!
 
reply
    Bookmark Topic Watch Topic
  • New Topic