Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble accessing list by index

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In JSTL, what is the correct syntax to access the first item of a list? I have



but I'm getting an error ... "0" (zero) property not found ...



- Dave
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Alvarado wrote:Hi,

In JSTL, what is the correct syntax to access the first item of a list? I have



but I'm getting an error ... "0" (zero) property not found ...



- Dave


Dave, this error means it the code thinks that divwords is supposed to be a string and there is no property called "0" in an instance of String. I think it is because


should instead be:



and, why not just use the following to make it less confusing:

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this reply, but when I change to your suggestion



nothing is getting printed out at



producing this ...



So if I were to add other items onto the list, I wouldn't use apostraphes, just commas -- e.g. "${Overview, Details, Special Offers}?

Also, note that the forEach loop is not looping over the "divwords" list, but rather the "divNames" list, otherwise, I'd change the loop to act as you suggested. - Dave
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now that you mean "${'Overview'}" to be a string array with one element. The code thinks this is a string.

please see this previous thread for your answer:

http://www.coderanch.com/t/290146/JSP/java/ForEach-array-data
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at the previous thread but he declares his string array via a scriptlet and I don't want to do that -- preferring the JSTL route. So if this is not the way to declare an array of strings ...



What is? Thanks, - Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "Overview" and what are you trying to set divwords to?

A little clarity might go a long way to solving this issue.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question on the table is how to declare a string array in JSTL. My empty searches on the Internet are starting to lead me to the conclusion it's not possible. For now, I have this ugly scriptlet ...



If someone knows how to write that as JSTL, then you are hired! Thanks, - Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't a way. That would be a grave misuse of the JSTL/EL which is intended as a display mechanism. Constructing data constructs is the job of the controllers, not the views.

Is the page needs this array, why is it not being constructed in the page controller?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!