jQuery in Action, 3rd edition
The moose likes JSF and the fly likes Populate Backing Bean Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Populate Backing Bean Exception Handling" Watch "Populate Backing Bean Exception Handling" New topic

Populate Backing Bean Exception Handling

James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
I am populating an h:selectOneMenu from the getter of a managed bean. This getter class calls the database and retrieves the list of f:selectItems for the drop down box.

2 Questions
1) Is the getter of the managed bean the right place to be doing this? It just feels like I am basically calling the database directly from the view...

2) If this IS the correct place to be calling the database...how do I handle any SQLExceptions that are thrown? I have tried to catch the error and redirect to an error page but I get an IllegalStateException.
Andreas Schmitt

Joined: Oct 17, 2005
Posts: 2
Hi there,

1) Sure u could do this in your getter, but mostly u have an own class to do this. In this class u could fill the properties of your "old" class.

So u could do the db connection in a class called db.java.
By pushing the button, u could call an methode from this calls which does the
db - connection an fills in the properties of your "old" calls ( the class which contains the getter and setter )

The exception handling is done in db.java
If there are any Exception u can catch them in this class.

Hope i could help u?
James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
Thanks for the reply, but in this scenario I am trying to decide how I should architect this correctly when I am navigating to a page for the first time which contains a dropdown of information pulled from the database. In this scenario, there is nothing "returned" from a getter so in case of error I can't return a string "error" for faces-config to then handle where user is directed.

What I have decided to do for now, is I have put a try/catch block around the code inside the getter (which will be pulling the data from the database for the dropdown) and if there are any exceptions caught I can then explicitly redirect the user

(Before this redirect wasn't working because I had flush="true" in the jsp page that contained the f:selectItems for the drop down).

I am very interested to see the general architecture of how any JSF veterans (or other newbies) are handling database connections/exception handling for a page that needs to retrieve data from the database when it loads for the first time.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Did you find out if there was a way to do the redirecting after form submission?
James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
The way I redirect after form submission is through a combination of the method that is called when the user submits a form and the navigation-rule's in faces-config.xml.

Basically I set things up like this:

You've got pages with forms. Correponding to each page I have a managed-bean that has getter and setters for each control in the form. If the control is populated through the database (as in the case where it's a dropdown box generated from the database) then this happens in the getter see this link for more information on that.

I have a managed bean that will act as the "controller" and it is named "emailPreferencesController" in faces-config. This managed bean has several methods including "updateXXX" and "retrieveXXX". From each of the forms, if a user clicks to submit the form one of these methods will be called. For instance if the user is on a form to update a persona then the code surrounding the submit button will be:

Which will submit the form (values will be put into the managed bean for the form) and the method "updatePersona" will be called from the "emailPreferencesController" bean. If this method is successful, the string "success" is returned and faces-config.xml will route you to a success page. If there is a validation problem the string "failure" will be returned and faces-config will route you back to the same page where you can fix the values you input.
I agree. Here's the link: http://aspose.com/file-tools
subject: Populate Backing Bean Exception Handling
It's not a secret anymore!