• Post Reply Bookmark Topic Watch Topic
  • New Topic

c:forEach and runtime "end" expression  RSS feed

 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following jsp snippet, I'm trying to use the length of an array to set the limit. The reason, as can be seen from the body of the <select> is so I can use the index value in the array for the as the value sent when the user selects this.


I get this error:

javax.servlet.ServletException: The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.

So, it's evidently not evaluating EL.

What is wrong? Everything I read says end should be able to take runtime values. What am I doing here?

TIA
 
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
So, it's evidently not evaluating EL.


Invalid assumption.

${userlist.length} is not a valid expression.

To get the length of a collection, you'll need to use the fn:length() JSTL function.

But I wouldn't do it that way anyway. I'd use items rather than begin and end, and varStatus to get the current index.
[ September 23, 2006: Message edited by: Bear Bibeault ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The length field is a variable in an array object and not a function unlike its String counterpart. Like Bear suggested i would use items and varStatus to loop through the thing. Begin and end are 0 indexed. varStatus is not.
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'd like to think great minds think alike;-). After posting, I went off and discovered varStatus (I'm a bit new to this) and did exactly as you two scholars suggested.

However, I am somewhat taken aback by John's comment. varStatus is *NOT* zero-indexed, like every other array index-related thing in Java, C++, C,...?

Although I was using the idea, I haven't checked and tested it yet, and this comment came as a big surprise. It was not pointed out in the Sun Java online API reference, either, that I have become oh, so intimate with.

Big thanks!!
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, in fact, I just went and checked. Evidently, what you get with varStatus is a LoopTagStatus object, which has a getIndex() method. The way I set it up was to reference this variable in the EL thusly (as I said previously, I haven't tested this yet):



inside a select block. So, I just went to the Sun API site:

http://java.sun.com/javaee/5/docs/api/

and, under the LoopTagStatus link, found this:

getIndex

int getIndex()

Retrieves the index of the current round of the iteration. If iteration is being performed over a subset of an underlying array, java.lang.Collection, or other type, the index returned is absolute with respect to the underlying collection. Indices are 0-based.

Returns:
the 0-based index of the current round of the iteration

So, this says it returns the 0 based index. Are we talking about the same thing?

Thanks again.
 
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
index is 0-based, count is 1-based.
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! OK. Thanks so much. Once again, my undying appreciation!
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
index is 0-based, count is 1-based.


ahh yes. I forgot to mention i was talking about count. That should have read " The count property in varStatus is not." sorry
[ September 24, 2006: Message edited by: John Meyers ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!