Forums Register Login

Submit an array of objects? Is there a 'standard' JSF way how to do this?

+Pie Number of slices to send: Send
Hello,

My problem is very simple. I have a list of objects in my bean (ex: List<Apple> applies = new ArrayList<Apple>() ).
The object has several fields (ex: supplier, color, width, height, breadth, etc.)
I want to show this list on the front end. However I also want to allow the user to edit the attributes of the apples.

So from the front end I will have like a list of fields where a set of fields is related to a single object in the list.
I would also like to add/delete apples.

What first came to my mind is to map every field in the object Apple to a List. For example if Apple has field suppliers and field color then in the bean I would create two Lists, List<String> supplier, and List<String> color.
From the front end I would display the contents of these Lists rather then the List<Apple> apples.
The name of the field would be the same for each set of attributes.
On save (form submit) the Lists would be re-populated with the field values (changed or not) and then I would be my Apple objects from the bean before saving in database.

However I am not sure if there is something in JSF that can help me achieve this in a simple way, working only with List<Apple> rather than adding additional Lists.

1
+Pie Number of slices to send: Send
If what you are talking about is a homogeneous table with fixed rows and columns, just bind the list of Apples to a JSF datatable tag. When you submit the form containing the datatable, all of its input controls will automatically update the list (Model), providing that all items on the form have valid values.

If the table is heterogeneous (rows have different sets of columns), there's no simple way to do that, since you'd need to set up a table with dynamically-defined rows. Which isn't too hard to do if you use a control binding, but it does require logic in the backing bean to properly layout the various controls within the display structure. Once that was done and the backing data model properties mapped to their respective controls, once again JSF would automatically transfer data from the model to the view and automatically transfer changes from the view back to the model.
+Pie Number of slices to send: Send
 

Tim Holloway wrote:If what you are talking about is a homogeneous table with fixed rows and columns, just bind the list of Apples to a JSF datatable tag. When you submit the form containing the datatable, all of its input controls will automatically update the list (Model), providing that all items on the form have valid values.



Do you mean like open faces data table? So if I loop through a List of Apples, and the List has 4 apples. I change the color of the first apple, and add a new apple (therefore I have five apples on screen). When I save (submit), will the List of Apple(s) have five apples and the color field of first apple will be updated. That would be fantastic for me.

Tim Holloway wrote:
If the table is heterogeneous (rows have different sets of columns), there's no simple way to do that, since you'd need to set up a table with dynamically-defined rows. Which isn't too hard to do if you use a control binding, but it does require logic in the backing bean to properly layout the various controls within the display structure. Once that was done and the backing data model properties mapped to their respective controls, once again JSF would automatically transfer data from the model to the view and automatically transfer changes from the view back to the model.



All rows would have the same number of columns in my scenario and all would be required.
+Pie Number of slices to send: Send
OK, this is one of the most common JSF tasks there is. There's a rather old, but still very useful example in a series of articles written by Rick Hightower for IBM DeveloperWorks. Google for "JSF for nonBelievers".

Although this article was written about JSF version 1, everything in it still applies other than now we use xhtml instead of JSP for View Templates. And, of course, you can use annotations on your backing beans instead of being forced to define them in the faces-config.xml file.
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1598 times.
Similar Threads
help with custom tags
Question on how to fix error on inputHidden field
Iterating over a list object to populate a 2D array
Iterating over columns and rows in a data table
Tutorial on Dynamic Attributes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:47:38.