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?
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!
Joined: Feb 15, 2005
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:
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!!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com