• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown

Populating Datatable from Database

Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Saloon Keeper
Posts: 26534
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
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic