Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Type Issue with EL  RSS feed

 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hey, that was it! Thanks a lot!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Done that more times than I care to admit
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&#117;


[ September 22, 2006: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
&#117;


Knowledge will set you free!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!