Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating Datatable from Database

 
Mike Boota
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am still learning JSF and need some help. I have a Page with one datatable and one command button to create a new record that takes to a new page where a user creates a record and the out come routes back to the dataable page. Whenever a user comes to a page datatable is populated with values from the database. Here is what I have done in the backing bean constructor I am calling a method that gets the data from database and populates the list that is the source for the datatable. Now if there is no data in the database I only see headers. Now if an error occurs while connecting to the database I want to show an error on the same page and below the empty datatable with command button, how do I handle this.

Next when a user clicks on a command button I have an associated action listener in the backing bean but some how it always executes the constructor first and then the action listener method and if the database fails it never executes the action listener. How do i handle this.

Can someone please help me what is the best way to do this. As when a user comes to page wants the datatable automatically populated. Is doing this in backing bean constructor the only way. Any help is appreciated.

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just define a String property in your backing bean that returns a status/error message and set up an output text element in your JSF page to display it. At its simplest, you can just return an empty string if there's no error, or if you prefer you can add additional properties to control whether the output text item is displayed at all if empty, select a highlighting style based on error severity, etc.

Of course, for that to work properly, you have to intercept the database exception and translate it to a message string, since if the exception blows all the way back to the JSF framework you end up with the user seeing an ugly/useless stack trace page, instead.

In addition to catching exceptions, of course, you can set a message string to something like "No Data Available" if the datamodel has a row count of zero.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic