wood burning stoves 2.0*
The moose likes JSP and the fly likes forEach nested loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "forEach nested loop" Watch "forEach nested loop" New topic
Author

forEach nested loop

Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Hello, just looking for a bit of advice on how I might approach this problem, which is a simple one but I just can't get it to work using Spring Hibernate and JSTL.

What I am trying to acheive is a group result output from 2 tables. Table Frameworks has a one to many with Ammendments.

Now what I want, on the same page, is this



I have tried to put all the frameworks and all the ammendments into 2 seperate hash maps in my controllers referenceData method and then use forEach in the JSTL to access them, with no success, like this (and in a variety of ways) with no success:


I also tried to nest the ammendment hashmap into the framework hashmap in my referenceData method like this: (but I don't know how to access this in the JSP page???)


This second way makes more sense to me as I am just using all my spring objects and methods. But how to get the result on the otherside???

Or maybe there is a simpler way with Hibernate?

Thanks for reading.

cheers
Martin

I
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60817
    
  65

OK, you've thrown a lot of red herrings around like Sring and Hibernate and other terms that, to me, are all just clouding the issue.

The only thing that matters is the data structure that is being passed to the JSP page for consumption by the JSTL and EL.

Factor all that other stuff out and figure out what you want to do on the page. Then work backwards from there and figure out what data structure you need in order to achieve that.

In other words: don't let the dog wag the tail. Tailor the data structure to the needs of the page rather than try to perform contortions on the page because you passed a gnarly data structure to it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Hello Bear.

Thanks for the reply.

What you describe is kind of my problem exactly. I have been requested to use Spring and Hibernate and am learning both through development of this project. I know what I need to acheive and whilst I am still at these early stages with learning I have decided upon a simple representation of the data from the database as a nested map which logically represents a grouped result to me.

So I have a map of frameworks and within the framework map I have an ammendment map, there is framework data in the framework map and ammendment data, related to the parent framework, in the ammendment map. This is now what I need to output.

So my question is how can I forEach over nested HashMaps?

You can see how I am placing the data into the hashmaps in my original post. I may well change the way that I get the data from the database in the future but for now I just need to get this up and running and HashMaps are the way for me.

P.S. is there anything in JSP || JSTL that will just dump the contents of a hashmap?

Thanks for reading.

cheers
Martin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60817
    
  65

Your (uncommented) code to build the nested maps looks very odd to me and it might not be doing what you think that is doing...

But putting that aside, if, when it all comes doewn to it, you just want to iterate over a map whose entries are themselves maps, a simple set of nested <c:forEach> actions is all that is needed.

Bear in mind that when iterating over a map, each item through the loop will be an instance of Map.Entry which in turns contains the map key and the corresponding value.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
As Bear said your code looks a little confused.
Particularly with your hashMap.put calls.

>frames.put("frameworks", framework);
This will put the current framework into the map under the key "frameworks"
However on the next iteration through the loop, you will overwrite it with the next framework, as you are using the same key.
The whole point of a Map, is that the key is unique, so that you can use that key to look up objects in the map. Framework id would be ideal here.


I think something like this will work:



At the end of this you have two things
1 - A list of frameworks
2 - A map of amendment lists, indexed by framework id.

The JSTL code then becomes:



The outer forEach just iterates through the list of frameworks.

For each framework, we take its id, and get the List of amendments related to that framework from the Map we have. (inner forEach)

I'll leave the formatting and tidying up to you ;-)

Cheers,
evnafets
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Excellent. Thank you both for your replies.

Stefan Evans thank you very much for your assistance I see what you mean and it makes sense to me thank you for explaining something so clearly. I will give that code a go.

cheers
Martin
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Ok I think I am getting close there are just a couple of things that I cannot crack, this should be so simple I can't believe it is now 2.5 days to get this working.

Anyway in my class I now have the following to create the map


In my JSP page I can access it like this:


This is all good and gets the value correctly but only proves that the data is actually in the map. What I now need to do is loop over the frameworks and get the ammendments relative to the frameworks. I can get values like this:


But that is incorrect as I am not looping over the specific ammendments related to the frameworks which produces holes in the data. So in theory I would need to do this (below) but this gives me no results at all from the ammendment loop, just blank.


I have run a test against the class itself and the map is populated correctly. What I think I am missing is the correct way to loop, or interrogate a Map. What could possibly make it work? Or what could be wrong?

Thanks for reading, have a nice day.

cheers
Martin
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
OK well I got it working using plain old JSP but this kind of defeats the purpose of what I am trying to achieve. I just need to do the same thing as below with JSTL and not referencing the objects and their methods.

Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Oh, the joys of EL and Maps.

I'm gussing my code didn't originally work because framework.getId() returns an int? You can't put an int into a map. Your solution was to turn it into a String, which you CAN put into a map.

However EL would be evaluating ${framework.frameworkId} as an Integer object.
Integer != String so any lookups on the map fail. ie the number 1 is not equal to the string "1".

Solution: get the types in EL and the Map to match, and it should work.
Try this in your java code constructing the data structures.


And then the JSTL code that I had in my previous post.



By putting an Integer as the map key the variable types should match (fingers crossed) and the JSTL code should work straight off.

--------------------------
As an alternative solution, instead of making the Map keys Integers, we could try and make the JSTL variable a String:
So leave your Java code as is (with Strings in the Map) and put the following on the JSP:



The <c:set> using the body of the tag to evaluate will result in a String.
Since String == String the map lookup should work this way as well.

Hope I haven't confused you too much ;-)
Cheers,
evnafets
Martin Thorpe
Ranch Hand

Joined: May 25, 2004
Posts: 45
Wahey Stefan, thats the doozy (don't know if I spelt that right probably as bad as my spelling of amendment ;-).

The thing is working as expected. Thank you very much for your in-depth help it is much appreciated. Is it always this difficult when you try to do something with Maps and EL?

Is there another way to deal with results of data in the JSP page, such as some kid of resultset output or something as opposed to a map?


Anyhoo, this is the implementation in the end.

The code in the servlet stayed the same as before casting the int of the frameworkID into a string to put into the map


And then in my JSP I am creating the string by using the c:set tag as you suggested


Stefan again thankyou very much I have certainly learnt a lot in this few day adventure outputting a grouped result. Thank you for sharing your time and your knowledge it really is much appreciated.

cheers
Martin
[ August 11, 2006: Message edited by: Martin Thorpe ]
 
Consider Paul's rocket mass heater.
 
subject: forEach nested loop
 
Similar Threads
What's the benefit of using those available Java frameworks
need Suggestion
Spring vs. Struts
Does Frameworks Addresses all the issues related to Web Application development
SpringMVC and Transactions