This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes display hashmap values in jsf xhtml 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 » JSF
Bookmark "display hashmap values in jsf xhtml" Watch "display hashmap values in jsf xhtml" New topic
Author

display hashmap values in jsf xhtml

A Rene
Greenhorn

Joined: Jul 17, 2013
Posts: 4
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

Joined: Feb 22, 2010
Posts: 19
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

Joined: Jul 17, 2013
Posts: 4
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

Joined: Feb 22, 2010
Posts: 19
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

Joined: Jun 25, 2001
Posts: 15961
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
A Rene
Greenhorn

Joined: Jul 17, 2013
Posts: 4
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

Joined: Jun 25, 2001
Posts: 15961
    
  19

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display hashmap values in jsf xhtml
 
Similar Threads
Updating a form in primefaces when a selection is made in the datatable
Data table header issue
Trouble updating primefaces component w/ajax
Sorting is not working in my JSF PrimeFaces Datatable and blank (empty) rows are displayed filtering
problem with hibernate