• 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

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

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27752
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
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic