• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

database value in backingBean

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 28401
210
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
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.
 
Johan Rignas
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think I solved it with just a conversion inside my previous updateClicked():

 
Johan Rignas
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28401
210
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
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28401
210
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
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.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic