• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nulls being added to database instead of actual values

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am trying to update my database with the values in input fields. The problem I am having is that nulls are being added to the database instead of the values in the fields.
Here is some of my code:

My JSF code:


My Managed Bean's update method:


Any suggestions would be a big help. I have been stuck on this for a few weeks now.
 
Saloon Keeper
Posts: 27763
196
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 the abstract, I don't see anything wrong with that. But it does depend on the wider context.

JSF doesn't set String property values to null from incoming forms requests, if for no other reason than that null is a binary value and JSF forms are HTML (text). So there's evidently a disconnect somewhere.

The first question, then, is: Is your backing bean Request Scoped?

Because Request Scope is almost useless in JSF. Usually you'll need at least View or Session scope.
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

My backing bean is View Scoped
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
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
Next question: Are all of those controls inside an <h:form> element? JSF controls cannot function outside of a form.

Incidentally, all that raw HTML isn't really a good idea. Consider using a panelGrid instead of a TABLE to handle the layout.
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, everything is inside a form. I tried a panelGrid initially but I don't know how to use that to get the functionality I need. For example, my ui:repeat has #{clientsPageManagedBean.selectedClient} for its value, how can I do the same with a panelGrid when it does not have a value attribute? Or do you mean to use a panelGrid within my ui:repeat?
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed it to use a panelGrid with ui:repeat. However, I am still having the same issue.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
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
Actually, it looks like your update method works on a scalar (single) instance object of data, but the View is expecting to be working with a collection of objects. Specifically, an array or list model accessed via the backing bean property named "selectedclient".
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do I need to return something else from my update method?
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
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
The update method returns the navigation token that says what page to display next. There's no need to change that.

But the data the update method is using doesn't match the view definition. The view is defining what should be effectively an array and the update method is attempting to obtain data from a non-array object.

In the case of a normal JSF datatable, you could use a DataModel to determine which specific object within the View's array that you wanted to get your update data from. However a dataTable expects the data to repeat in rows and not in columns, and the ui:repeat tag doesn't have an equivalent sub-Model wrapper object class to the DataModel class used by dataTables, so you're going to have to supply a parameter to the update method that indicates which element in the selectedClient array you want to update from.
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I pass the clientID to the method so it knows which row in the database table to update, is that what you mean?
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
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
No. First of all, action methods aren't supposed to have parameters. They are supposed to use the Model to get their data.

Secondly - and more importantly - it's not the database row that you need to uniquely locate, it's which GUI controls you get the update data from. You have set up your View to have multiple instances of the "selectedClient" object, each with its own unique set of GUI controls. The backing bean cannot tell which one of those control sets contains the data to be used for the update.
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you point me in the direction of a tutorial that will teach me how to do that because I don't even know where to start
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
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 need to start by understanding the fundamental difference between a single object and an array of objects. That's not JSF, or even web-related. It's basic programming.
 
Oli Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do know the difference between them
 
reply
    Bookmark Topic Watch Topic
  • New Topic