This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This has been driving me crazy for two weeks. It's probably something really stupid or obvious, but for the life of me I can't get the page to render a collection of child objects.
Here's the basic outline, stripped down to what I hope are the essentials:
1. In the backing bean, I have an ArrayList<> property with a getter:
Note that the data returned may be obtained in a couple of different ways. Either way, however, it's an ArrayList.
2. The SurveyTopic class looks like this (again, excess stuff removed for clarity):
Note that it contains a member ArrayList of another object type.
3. I want to display the SurveyTopic objects in a dataTable, with each object's list of SurveyReponse objects listed in a single column of the dataTable:
Now, to the crux of the matter:
When the page displays, the dataTable renders fine, but the column that's supposed to show the surveyResponses objects is empty (the header facet displays, though).
Some more notes:
1. Yes, there is data in the embedded ArrayList. I can see it in the debugger.
2. I originally had the surveyResponse objects displaying in an embedded dataTable, beneath each row of the main dataTable. The same thing happened, though: the main dataTable displayed fine, but there were no surveyResponse objects at all on the rendered page. It's as if that code wasn't even there.
3. A breakpoint in SurveyTopic.getSurveyResponses is not hit. It looks like JSF does not translate into a call to the getter. I confess to some uncertainty over exactly how JSF does this, and over what the allowable syntaxes are.
4. The same thing happens if I change to above.
5. All the data is coming from a MySQL database, via Hibernate.
Any ideas or suggestions are most welcome.
Thanks in advance, and thanks also for creating and running this forum. You can hardly search for Java programming-related information on the web without getting referred here.
I'm afraid the longer the thread, the sloppier I read it, but here are some general observations:
1. If you're using an ORM and the parent object is detached, make sure that the child objects were fetched before detaching. Usually you'll get something like a "lazyfetchnotinitializedexception", but whatever you get, it won't be what you wanted.
You should not expect to get data properly using any of your attempted reference EL expressions. You need 2 things: first, the EL must reference a property, NOT a method call. secondly, the property referenced must not be a collection, it must be a JSF table model object. JSF2 seems to be able to construct a default one, but the rules are unclear, so it's a lot better to do it yourself. JSF1 didn't even attempt to fake it.
Also, of course, your backing bean containing the DataModel object must NOT be request-scoped, because the cursor information gets wiped out between requests. Use a minimum of View scope (JSF2) or Session scope (JSF1).
So here's the way it should look:
And the backing code:
An IDE is no substitute for an Intelligent Developer.
Joined: Nov 20, 2011
Thanks for the feedback and suggestions. I'm afraid, however, that even after implementing the code as you suggest, the behavior remains the same. That is, the dataTable itself renders fine, except that the last column containing the list of objects in the embedded collection shows up as empty. I won't belabor this by posting all the code, since it just mirrors what you posted.
Your thought about lazy loaded objects is quite relevant given I'm using Hibernate, but in this case I can see everything in memory at a breakpoint inside getReportSurveyTopics(). The bean is session-scoped, so that doesn't account for it either.
I'm not sure what's next here, and suggestions are of course most welcome. I find myself wondering if there's any kind of debugger-like tool that lets you watch the rendering process, to see what happens when that embedded collection is supposed to be picked up.
Jerry Hatch wrote: That is, the dataTable itself renders fine, except that the last column containing the list of objects in the embedded collection shows up as empty.
Oh crud. You're using JSTL. You must have missed all the other places where I warned people NOT to use JSTL.
I did mention I read long posting pretty sloppy, which is why I missed that little nuance.
The proper way to render would be to embed a dataTable for that cell within the outer dataTable. That means that the outer DataTable model's "surveyResponses" property would also be a DataModel object.
Joined: Nov 20, 2011
Thanks again, Tim.
Unfortunately, that's what I tried originally (an embedded dataTable, with rows right under each row of the original dataTable), and the same thing happens. The app has embedded dataTables in other places, and they work fine. I tried the JSTL as a last ditch shot.
I'll noodle with this a bit. One thing I can think of is, I'll try putting the embedded dataTable in a <h:column> of the outer one.
Further thoughts/suggestions are most welcome, of course.
Joined: Nov 20, 2011
Well, I'll be a monkey's uncle (or some such).
With a dataTable embedded inside the last column of the main dataTable, the data displays.
I will investigate why my former attempt at an embedded dataTable (showing separate rows under each row of the main dataTable) failed, but for now I can push forward this way.
Many thanks, Tim, for your guidance.