File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Best practice to get table form data back to Java beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Best practice to get table form data back to Java beans" Watch "Best practice to get table form data back to Java beans" New topic
Author

Best practice to get table form data back to Java beans

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Howdy!

If i have a collection of Java beans it's very easy to create a HTML table by using the JSTL <forEach> tag for example.

And after some trying i was wondering if there's a similar easy way to do it the other way around. Let's assume i have a table of text input fields populated with one Java bean per row so the user can change some values in the input fields and submit the updated form data.

What are your best practices to update the corresponding properties in the underlying Java beans? Is there a better way than to use getParameterValues() for example and cast all values of the String array to the property types of my Java beans? And how should I identify which table row belongs to which bean in my collection? Should i introduce something like a "primary key" for my Java beans?

By the way i know i could use a framework like Struts for this but i first want to learn how to do it "by hand".

Thanks for your suggestions!

Marco
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
There are a few ways to do this. Before I answer can you explain what you mean by :
_______________________________________________________________________
Is there a better way than to use getParameterValues() for example and cast all values of the String array to the property types of my Java beans?
_______________________________________________________________________
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

OK, this sounds good. I hope i can explain what i mean...

If i use the usual methods of my request object in a servlet (for example getParameterValuse() or getParameterMap()) to retrieve form data within a HTML table of text input fields the bottom line is i will get String arrays for each table column containing the values of this column for all rows, right?

Then (at least in my understanding) i have to manage two things. First i have to identify which table row of input fields corresponds to which Java bean object in my collection so that the correct properties of the correct bean get updated.
Second i have to cast each parameter value to the right type of a bean property because i only have an array of Strings.

Of course this is a way to do this but i can imagine there's a more elegant way to achieve this in JSPs oder servlets. Just a simple solution like using EL or the <jsp:useBean> tag to get a collection of Java beans INTO my table or input fields.

I hope now it's a bit clearer what my problem is. I'm just searching for a better solution to this problem.

Marco
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Personally, I've always done this by working a row number into the input field names.



Then, on the server side, I can build up my list of row beans in a loop.
To determine the length, you can either put a form field in the page ("row_count") or loop until a particular field is null.



It's probably not the slickest way but it's very explicit.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Thanks Ben, at least I know that I'm not missing some essential JSP tricks if you do it a similar way ;)

If i would use the alternative way with getParameterValues() and String arrays can you tell me if it's guaranteed that the order of my values in the array is the same as the values appear in the table? I think it would make most sense but I'm not sure about this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Frankly, I've never learned whether or not the order is guaranteed because I've never like the idea of working a tables represented as vertical arrays (columns instead of rows).

Also, when debugging, I like being able to look at the HTML source to see exactly what the field name is going to be, see exactly what the value is going to be, and then match that up with the generated table on the server side.

Again, there might be a slicker way but this approach has always worked for me.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

From this point of view i think your solution will be the better way. I'm going to try it by using the name attribute of the input elements as a key to my Java beans. This sounds more reasonable to me, too.

I don't really know how the popular frameworks handle these problems, but I think you'll have to use XML descriptors or something like this?!? Anyway this isn't worth reinventing the wheel on my own.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
If you would use Struts and use indexed properties in your ActionForm then each row could represent one object with each cell representing a property of an object, then you would not have to use this HTML tricks of indicating which row a posted name/value pair came from. IF you do not want to use Struts then you can use the classes (from Jakarta Commons project ) to auto populate your collection of objects from the request parameters. This is how Struts populates their ActionForms
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Thanks a lot Michael!

This seems to be an interesting alternative. I'll surely give it a try if I can figure out how to use it ;)
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Which one you want to try - Struts or Commons?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

First I'd like to try the Commons library. I'm quite new to this subject and as I said it's primarily for learning purposes. For real world applications I would probably want to benefit from existing frameworks...

Could you give me an example how to use the Commons library for populating a whole list of beans? Of course i could read through the docs if it's not possible to explain this here in a short way ;)
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
I do not have an example handy. Try looking at the BeanUtils package. I think the method is BeanUtils.copyProperties(source, target). You will have to familiarize yourself with IndexedProperties and MappedProperties.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
I got this info from Jakarta Commons site

BeanUtils and ConvertUtils Conversions

A very common use case (and the situation that caused the initial creation of the BeanUtils package) was the desire to convert the set of request parameters that were included in a javax.servlet.HttpServletRequest received by a web application into a set of corresponding property setter calls on an arbitrary JavaBean. (This is one of the fundamental services provided by the Struts Framework, which uses BeanUtils internally to implement this functionality.)

In an HTTP request, the set of included parameters is made available as a series of String (or String array, if there is more than one value for the same parameter name) instances, which need to be converted to the underlying data type. The BeanUtils class provides property setter methods that accept String values, and automatically convert them to appropriate property types for Java primitives (such as int or boolean), and property getter methods that perform the reverse conversion. Finally, a populate() method is provided that accepts a java.util.Map containing a set of property values (keyed by property name), and calls all of the appropriate setters whenever the underlying bean has a property with the same name as one of the request parameters. So, you can perform the all-in-one property setting operation like this:

HttpServletRequest request = ...;
MyBean bean = ...;
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
BeanUtils.populate(bean, map);
Vinay Singh
Ranch Hand

Joined: Dec 15, 2004
Posts: 174
Struts may not be required. Use BeanUtils.
We use it both for setting values into java beans after reading from database and for setting values into beans(not always meant for view) after reading from result sets.
Very handy utility.


Technical quiz and interview questions   SCJP 6 mock practice test
 
 
subject: Best practice to get table form data back to Java beans
 
Similar Threads
Dynamic form table generation and naming of form fields
AJAX, JSF and security
is there something similar to c:forEach in JSF
How to determine when a form field is submitted if it's changed from the original value?
java bean vs. taglib: who should do the work?