• 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

logic:iterate usage improvement needed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to learn struts and I have created a small application that deals and shuffles cards to players. That part is working excellent...but now I want to display it and in my JSP page I have the following code - which does work...I just know there has to be a better way - but I'm stuck on ideas!

I have the following code in my JSP page:



This does exactly what I want it to do - it iterates through my playerName arrayList and then iterates through the array list that contains all the cards the player has in their hand.

This works just fine - if I know the player names. It just seems like this isn't the best solution. I would think that there would be a way to make this alot more dynamic. And not have to use the <logic:equal> tags to determine which arrayList to display.

I was reading about logic:iterate and saw that there is an indexID - but I'm not sure if this is something I could even use. Can you do IF statements in the JSP page? Like if this is the first iteration of the player name - then display playerOneHand ArrayList, if this is the second iteration - then display playerTwoHand ArrayList, etc?

Anyone have any thoughts on what I can do to improve this?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be able to make some suggestions if I understood your model a little better. Could you briefly describe the javaBeans you're using and their relationship?

For example:
Bean1
List objs contains Bean2 objects
int Id

Bean2
int Id
String name
Map myMap consists of name as key and Bean3 as value

... etc.
 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my action class - ActionForward execute

I have defined an arrayList for each players hands: playerOneHand & playerTwoHand

Then I created a string array that has the player names - playerNames

Should I be doing all this in the action? Should I have another bean or something?

What I want to do is display the first players name, the first player's hand, the second players name, the second players hand.

I thought that if there was a way within Struts to tell which iteration I'm in with the logic:iterate tag, I could iterate through the player names, and then the hands. If I could tell that I'm on the first iteration of the player names - then I'd display the playerOneHand arrayList, if I'm on the second iteration - I'd display playerTwoHand ArrayList. Is that even a good idea to try to do? I'm kinda doing that with my previous code - by using the <logic:equal> tags - but I have to know the player names - and that's not always going to be the case.

Am I making sense? I'm still pretty new to struts and java...and I've been given the opportunity to learn all this - basically on my own and man it's tough!! So any thoughts are greatly appreciated!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I'd suggest:
  • Instead of having two separate lists: one for player names and another for player hands, I'd create a single HashMap (or TreeMap if they need to be in a certain order) with player name as the key and player hand as the value for each player.
  • Iterate over the hasmap using the properties "key" and "value to get the key and value pairs from the Map.


  • Here's an example:

    <logic:iterate id="player" name="playerMap" scope="request" />
    Player name is: <bean:write name="player" property="key" />
    Player hand is: <logic:iterate id="card" name="player" property="value" />
    <bean:write name="card" />
    </logic:iterate>
    </logic:iterate>

    See the following reference for an explanation of the special "key" and "value" properties used for Map objects:

    http://struts.apache.org/userGuide/struts-logic.html#iterate
    [ May 11, 2005: Message edited by: Merrill Higginson ]
     
    Tina Long
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    SWEET! It works beautifully!! Thank you so much for your input...I do appreciate it! I knew that there had to be a better way to do what I wanted to do - I just don't know Java well enough to come up with those things - I read about hashmaps before but I didn't think that was what I needed - I guess I didn't understand it fully - but now I see how it works...so THANK YOU!!
     
    my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic