• Post Reply Bookmark Topic Watch Topic
  • New Topic

Referencing a specific item in an arraylist without a scriptlet

 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good afternoon everyone.

I have the following bit of code in my application which works fine:

<c ut value="${TestBean.lineItemList[0].foo}" />

lineItemList is an ArrayList of LineItemBeans. There is a field called "foo" that I am trying to aquire...and and "id" field as well.

Now the problem is that I will not always be referencing the [0] item in the list. And I don't want all of the items in the list. Just one...specifically the one associated with the specific "id" field that equals a request variable id that i've passed in that we'll call "requestId". so i kind of need to do something like this:

<c ut value="${TestBean.lineItemList[item with id of "requestId"].foo}" />

What's the best way to go about this? Do I have to use a loop, testing for the id's to be equal? That seems like it's too much code and that there should be a simpler way. And can I do it without scriptlets...that's the big question?

Thank you very much!!
b
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you not just go ahead and try:



It should do exactly what you want.

A few notes:

1) Checking the 'disable smilies' box on your posts will make your <cut> tags look less surprised.

2) Convention dictates that TestBean should be testBean -- normal Java naming rules apply.
[ December 23, 2004: Message edited by: Bear Bibeault ]
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though I was wrong twice last time, I'll try it again.

Well, I have tried that actually...but that won't work because the requestid is equal to the lineItemList[i].id field, it's not the index itself.

notes:

1) excellent idea
2) unfortunately i can't change the coding standards mid project. however, it is duly noted for a code review!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the index?
[ December 23, 2004: Message edited by: Bear Bibeault ]
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's the problem...we don't know the index.

the id is coming randomly from a hyperlink on the previous page.
the index could be anything depending upon what id that we have.

so i was thinking i'd have to loop through the lineItemList until lineItemList[i].id == requestId. However, there has to be a better way?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds more like you need to step back and reorganize the data that's being sent to the JSP in a manner more suited to its use.

This is a common error in the JSTL/EL environment: the data is sent to the page in a manner that is convenient for the controller servlet to set up rather than having the controller structure the data in a manner well-suited for the page.

Do the contortions in the Java code, not on the JSP.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!