• 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

How to determine when a form field is submitted if it's changed from the original value?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

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.

Thanks.
 
john lazeraski
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please concise your question
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic