Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

display hashmap values in jsf xhtml

 
A Rene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Im trying to display hashmap values(key and value) in jsf xhtml. can any one suggest me how to achieve this. Also I made an attempt in the following way which results in fail. Any help would be highly appreciated.

my xhtml part:



my controller :


my facade:


If I use the above code, I get the below error:
SEVERE: Error Rendering View[/pages/customer.xhtml]
javax.el.PropertyNotFoundException: /pages/customer.xhtml @43,90 value="#{static.desc}": The class 'java.lang.String' does not have the property 'desc'.

rgds
street
 
Prabu Pidaran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the exception, it looks like the "items" is a list of string from which you are trying to get the 'desc' and 'info'.
'items1' should be a list of object which contains 'desc' and 'info'.

Could you please check the values in 'items1'. ??
 
A Rene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Here is how I declare items1;

Let me explain how it is working.
If I change my query to means only one column and in my xhtml if I give instead of "static.desc" I can get the output. So I have problem when there are 2 columns. Please guide me how to display key and value if not using arraylist in xhtml.

Thanks in advance.
 
Prabu Pidaran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly try the following when you are selecting two columns in the query.

<p:column width="150">
<f:facet name="header">
<h:outputText value="#{bundle.d_label}"/>
</f:facet>
<h:outputText value="#{static[0]}"/>
</p:column>
<p:column width="150">
<f:facet name="header">
<h:outputText value="#{bundle.d_value}"/>
</f:facet>
<h:outputText value="#{static[1] }"/>
</p:column>
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabu Pidaran wrote:From the exception, it looks like the "items" is a list of string from which you are trying to get the 'desc' and 'info'.
'items1' should be a list of object which contains 'desc' and 'info'.

Could you please check the values in 'items1'. ??


Actually, "items1" should be an instance of javax.faces.DataModel. For a list, that would be a ListDataModel.

If you reference a raw collection as a value property on a JSF2 dataTable, an anonymous DataModel will be built as a Decorator object for the collection and the dataTable will use it. That's OK as long as you want display-only, but if you intend to do things like put command buttons or links into the table rows, the anonymous model object won't be accessible to obtain the extra properties that are used to figure out which row was being referenced.

Backing a dataTable directly with a collection in JSF1 would result in a runtime data type error.

Also, periodically people want to use a hashmap as a collection object for a JSF dataTable. This doesn't work, since the model for dataTable must have a discrete linear ordering in order to produce predictable results and a HashMap is a random-access collection with no inherent ordering. So to assure the necessary row ordering you need to provide some sort of adapter that enforces order on values returned from the HashMap. This can either be done by sorting the items into a List object or using a Map class that supports ordered retrieval such as a TreeMap.

Oh, and by the way. You don't write Controllers in JSF. The Controllers are pre-written and supplied with JSF itself. Backing beans are Models.
 
A Rene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
Also, periodically people want to use a hashmap as a collection object for a JSF dataTable. This doesn't work, since the model for dataTable must have a discrete linear ordering in order to produce predictable results and a HashMap is a random-access collection with no inherent ordering. So to assure the necessary row ordering you need to provide some sort of adapter that enforces order on values returned from the HashMap. This can either be done by sorting the items into a List object or using a Map class that supports ordered retrieval such as a TreeMap.


Thanks for reminding me this on hashmap. But in my case even though this does not matter I plan to use that But I have managed to get the output with namedquery by changing the passing arguments in xhtml to controller. Initially I did not get that idea and was trying to write native query thats why I have that issue.

Thanks again for all.

rgds
Rene
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I said, a JSF backing bean isn't a Controller, it's a Model. And View Definitions shouldn't "pass parameters", because they are intended to be templates, not logic repositories.

You can do what you want to do just by manipulating the Mode, but, as mentioned previously, you do need a sub-model (DataModel) to act as an intermediary between your raw data and the View Component (dataTable). Otherwise you'd have to code JSF-specific properties into the collection itself, which defeats JSF's POJO-oriented philosophy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic