• Post Reply Bookmark Topic Watch Topic
  • New Topic

Showing results of dynamic database query in jsf

 
ajaay shereen
Greenhorn
Posts: 3
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Sorry if i am posting for a previously answered question or a basic one. I am a newbie to jsf.
I want to process a query passed at runtime through webpage and display the results in another page. i have tried passing resultset directly to the datatable tag and it didnt give me desired reults.
somewhere i saw that said to change resultset to result. that didnt work for all the query results as well.
please help me on this. please tell which is the right way to show database results in jsf

Thanks in advance.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, ajaay!

You cannot pass a ResultSet directly to a dataTable. DataTables display what they get from a DataModel object, so you need to construct a ResultsetDataModel and supply your resultset to it. You construct a dataTable tag to reference that DataModel object.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm curious, why does dataTable needs a DataModel? It works with Lists for example, so as long as you assign the entries to a list there shouldn't be any problem. Or am I misunderstanding the question?
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DataModel provides the cursor services that the controller uses to iterate the rows in the table. Among other things, it allows you to bind actions to items in the table and know which item was selected.

You can't supply a List directly to a DataTable. There's a ListDataModel class that can be used for wrapping Lists, however.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For binding purposes and additional functionality, yes, but for simply displaying the results of a query, a list is enough.



This works and accomplishes its goal.
 
Bart Kummel
author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick,

You are right. However, in the example in your last post, the dataTable component still wraps the list in a javax.faces.model.ListDataModel internally. Most of the time you do not notice this, but it is good to know that this is happening behind the scenes.

Best regards,
Bart
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! ... good to know then.

Thanks,
 
ajaay shereen
Greenhorn
Posts: 3
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for clarifying my doubt.
@Tim: I will try what you have said about datamodel and post the result tomorrow.
Bart,
From what you have said about list converting into listdatamodel internally, i take it that all the Collection types change to corresponding DataModels when using in JSF. Please tell whether it is so.
Thanks once again.
 
Bart Kummel
author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajaay,

No, not all Collection types are wrapped in a DataModel automatically. According to the JSF 1.2 specification, only java.util.List or array of Objects are autmatically wrapped. I just read that part of the spec, and it actually says:

The current value identified by the value property is normally of type DataModel.
However, a DataModel wrapper instance must automatically be provided by the JSF
implementation if the current value is of one of the following types:
java.util.List
■ Array of java.util.Object
java.sql.ResultSet (which therefore also supports javax.sql.RowSet)
javax.servlet.jsp.jstl.sql.Result
■ Any other Java object is wrapped by a DataModel instance with a single row.

So it should be possible to use a ResultSet, as you tried. However, I never tried this and I think it isn't common use to do so.

Best regards,
Bart
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!