aspose file tools*
The moose likes JSF and the fly likes Submit an array of objects? Is there a 'standard' JSF way how to do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Submit an array of objects? Is there a Watch "Submit an array of objects? Is there a New topic
Author

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

Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
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.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submit an array of objects? Is there a 'standard' JSF way how to do this?