aspose file tools*
The moose likes JSF and the fly likes database value in backingBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "database value in backingBean" Watch "database value in backingBean" New topic
Author

database value in backingBean

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30
Hi. Is it possible to get database values into a backing bean?

I tried with this:

ModelBean:


BackingBean:


The problem with this is that this gives me error(becouse I try to access modelBean in the cunstructor...):
Cant instantiate class: package.name.BackBean


I would like to get the data into BackingBean when it is created to be able to "update" the value later on. Any tips?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

You cannot (usually) make an EJB/JPB Domain Model Object be a JSF backing bean because - while it's technically possible - they operate in different ways.

More often you'd use a backing bean as a container that fetches (and updates) the actual EJB, which you can present as a property of the backing bean.

Note that attempting to load objects from a database in a backing bean's constructor also isn't a good idea. For one thing, your database connection is best not hard-wired into the bean, and you can't inject the connection (or entityManager) at construction time. It's better to either do the object fetches in a @PostConstruct or in a separate pre-display method.


Customer surveys are for companies who didn't pay proper attention to begin with.
Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30
Ok. Thanks. But what about something like this:

ModelBean:


BackingBean:


If I just could get the value of a textfield with a given id it seems all will be solved. Any idea how to do this?
Something like this:

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30
Think I solved it with just a conversion inside my previous updateClicked():

Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30
Showed up this gives me the old value of the textField and not the new one.

Now I wounder: is there any hints on how I by a textfield can update a given row in a database with Beans?

WCS(worst case scenario) is that I have to go: jquery -> servlet -> database for an update and leave all beans out of it
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

In addition to not being able to use an Entity EJB as a backing bean, injecting an EJB into a backing bean doesn't usually work well for the same basic reason: you usually want the same EJB for every use of the backing bean, and injection requires you to specify a specific object at compile-time. You can, however, inject a DAO or Session EJB into the backing bean and use it to retrieve the instance that you want to work on. Once you do that, then the page can reference the bean and its properties using EL.

Please note that JSF is all about doing things for you. There's usually something wrong when your action processor method starts mucking around with the FacesContext, since the bean properties are automatically updated by JSF itself before the action method is invoked. And if one or more items fail validation, the bean won't be updated and the action method won't get invoked, so you can always be sure that only data that passes validation goes to the action processor.

Here's a minimalist sketch of a typical EJB/JSF interaction. It's not intended to be usable "as-is", but it does illustrate the major points. It's designed to display a form where you enter a keyvalue as text, then retrieve the EJB for that key, then redisplay the form with a few ejb properties:


Now for the View definition:
Johan Rignas
Ranch Hand

Joined: Jul 17, 2011
Posts: 30
Thanks!! Tried to do the same thing as you and it worked!

But I would like the values to show directly. Becouse of this I made one extra view page. This is how it looks:

index.jsf(just to fill the textField):


backingBean(Session scope):


viewPage:


Any comments on how this can be improved are welcome.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

That's what I do as well. For CRUD operations, I usually have 2 methods used to prep a backing bean for edit and display and name them "beginAdd" and "beginEdit". One sets up a blank EJB to be added, the other fetches an existing EJB.

Because "beginEdit" normally takes a primary key (after all, you have to know which EJB to edit!), I usually inject the backing bean into a setup bean which implements the initial choice to create or edit and allows input of the key value. Then the setup bean's action processor invokes the beginEdit(key) method in the injected editor backing bean.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: database value in backingBean