The subject should hopefully explain it.. basically, I have to know when any of my several form fields have actually changed. Typically you use some sort of POJO form object and load it up with data to display on a page, and if the form is submitted, within the action class it gets the object and auto-populates it, so that by the time your action method is called, the updated values (if any were changed by the user) are already in the object. However, I have to take any changed values and send them to another server, so I am trying to think of the best way to figure this out. To add a little twist to this.. in an effort to avoid multiple objects of "similar" fields being created with a bunch of copying of get/set methods being done to take the form input and put it into say, an entity bean, I've got a nice way of combining all this into one object (DTO really) so that the STruts form auto-populates my model pojo, which is then passed to the entity bean as a backing model.. thus using just one object in memory from the front tier to the back. This poses a problem though with my current question.. how do I tell when any form field has been modified so I can collect all the changed values?
I've basically come up with two ways. One is to provide a super class that contains a list of changed fields, and in the setter methods of each pojo model, simply adding to this list either the changed data or some indicator of the field name or something. The other way is to build struts pojo form objects, use those in the action classes, and then copy the values to the entity beans directly (or to a DTO that is passed to the ejb tier), and simply try a compare on every single field for every request. I am leaning towards some way to flag each field change within the setter methods during auto-population however, since that seems to me to be the best place/time to do it and also avoids me having to use 2ndary objects and run compares on every object/field and copying the objects to entities, etc.
Would love to hear how some of you may determine when a field actually changes from its original value during a user editing some data like profile data.
Joined: Nov 14, 2011
Well I started on the use of an interface that each pojo could implement, basically returns a Collection of the object type I need to collect and send for changed fields. Each setter would add to the collection if the value passed in was different than the one currently there.. thus indicating a change.
Then I stumbled across another problem. Some of my fields are multiple fields to make up a specific field. For example, a email or phone field is generic, but then there is a drop down to select home, mobile or work. The problem is, the place I have to send the data to requires me to specify the specific matching field of theirs to my data (think of data translation.. my city is sent to their city, but they offer home city, work city, other city). So in the setter of setCity() it's possible that the selector of home, work or other is NOT yet populated (which is a separate selection field and setter method). I could write some hack code that in each of those methods it checks and when ever the "type" of field it is changes, to go update all the "changed" objects in the collection to match the service filed names that I have to send the data to. But that is a night mare of code to deal with for this.
So, it looks like what I need to do instead is to keep a separate "temporary" object that gets populated by the struts framework.. then when the SAVE or GO or whatever button is clicked, in the action class method that handles that, loop through ALL the fields submitted and check if any are different from the original (db bound) data. If so, at that point I can set the incoming changes to the appropriately matched service fields that the data goes to. Pain in the butt. I am hoping there is some better way to do this from someone out there that has had to deal with something like this before.
ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
subject: How to determine when a form field is submitted if it's changed from the original value?