• 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

Iterating over hashMap in facelets not working

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class called Target and there are 3 types of targets, cpu, mem, and interface. I use this method to sort the targets into 3 hashMaps. I have used debugging statements to verify there is actually something in these hashes.



I need to display the 3 sets of Targets along with their keys. This is the code I think should work:



There are just blank spaces where the key values are supposed to be. I've tried many different versions of this and nothing works. I'm out of ideas. Am I missing something obvious?

TDR
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, in CompSci, we tend to use the term "sorting" to mean "ordering", whereas in this case, you're actually parcelling out stuff. But that's just me being pedantic again. I do that because sometimes it's important to remember the difference. In this case, it means that the individual HashMaps have no natural order within themselves.

Which is part of your problem. The Java code


is not valid. First, because it's not ordered data - although you can enumerate unordered collections, too. But more importantly, there's no "X" type. Hashmaps contain key/value pairings and in Java there's no specific object type defined that encapsulates those pairings, so you have do do something more on the order of:


To get that done on a View definition, you'd need to replicate that functionality.

However, I don't actually recommend putting logic on Views anyway. It's much more in the spirit of MVC to create Model objects and use one of the JSF collection display tags to present the collections (hashmaps). Generally, that's a dataTable, but you're using RichFaces and it has a listing element that might serve better in this case.

In any event, it's VERY much not recommended to use JSTL in JSF. It bites.
 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for replying Tim.

Actually that "for" loop is iterating over a list of Target objects and not the HashMap.



You are correct that what I'm doing is parcelling out the objects to hashmaps.

As I've been googling and reading all over the place I think you are onto something when you say

In any event, it's VERY much not recommended to use JSTL in JSF. It bites.


I've come across some postings that suggest I am using the wrong JSTL namespace or something like that?

I'm on the verge of rethinking this whole strategy.

But surely there is a way in JSF to iterate over a hashmap in a facelet richfaces iterating component? A hashMap is not some kind of esoteric data structure. I come from the perl world and hashes are our bread and butter.

I think I will move this problem to the richfaces forum since marrying a hashmap to richfaces is what I'm trying to do.

TDR
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSTL/JSF problems have nothing to do with the XML (namespaces). They have to do with the fact that JSF and JSTL are 2 different technologies.

JSTL is designed to add functionality to JSPs, although there's a whole 'nother argument about coding logic on JSPs.

JSF started out as JSPs, but it evolved into something much different. The actual JSF component object model is a 2-dimensional graph (tree structure) which is fed incoming (validated) data, then eventually to a rendering engine (usually, but not always HTML). The linear approach of JSTL and even raw HTML doesn't fit in well with that. In JSF 1.0, you had to kludge your way around it using "verbatim" tags or all the non-JSF content tended to pile up in an unwanted corner.

Later versions of JSF handle that kind of stuff better, but there are still limits, so it's better to avoid JSTL. As I've said before, JSF has its own ways of handling stuff like that and doesn't need JSTL, especially since the JSF equivalents are - naturally - aware of how JSF works.

RichFaces is an extension to core JSF, so if a RichFaces custom component can offer something that core JSF can't use it, but often the core JSF tags are sufficient.

You really do need to lose the idea of "iterating" on a View. A rendered View is a static 2-dimensional object. If it contains a table, the table is likewise a static 2-dimensional object. If I printed it out and handed it to you, you would have absolutely no way of determining whether that table had been produced by iterating top-to bottom, bottom-to-top, middle-to-outside or even in a single parallel operation that generated all the rows at once. MVC is all about separating the logic from the display. You can see this in JSF, in that the model object for a dataTable can be (among other things) a Collection, but the actual enumeration of the items in the collection is not done in user code - the JSF Controller handles that. Typically, it uses an Iterator internally, but other mechanisms are possible.

The one thing that is peculiar to JSF is that complex data cannot generally be directly used as a Model. In particular, the dataTable needs a dataModel, since that construct not only manages the data being presented, it also maintains the cursor that allows such things as telling which row you selected when you click on a commandButton/commandLink within a table. You can front a HashMap with a datamodel like so:



I typed this in from scratch/memory, so expect some defects. In the case of 3 different hashmaps, I'd probably subclass ListDataModel to create my own HashDataModel class to minimize redundant work.
 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That example helps. I will probably try to implement something like that. In the meantime I simple created an arraylist of keys and am using that in my rich:dataList. I like your solution better. Not so hackish and I hadn't considering creating a DataModel to handle this. (I'm too much of a newbie to have thought of that)

One final comment. I came across an article that explains very well why not to use JSTL with JSF/Facelets. Anyone else struggling with this issue is probably lacking a fundamental understanding of these 2 technologies. This article helps illuminate the problem.

http://www.znetdevelopment.com/blogs/2008/10/18/jstl-with-jsffacelets/
reply
    Bookmark Topic Watch Topic
  • New Topic