• 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

Struts handling of multi-page form "wizards"

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I was following the Jakarta StrutsLive tutorial (downloadable from theserverside), doing the multi-page user creation wizard part when I ran into this:

The idea was to pick up user data on 2 forms, then after the 2nd, saving into the DB. A User object saved into session was used to store the data gained from the first form. The same ActionForm (named userForm) was used for both form pages, and the data transfer between the userForm and the User object was handled by Jakarta Commons' BeanUtils.copyProperties() method, which copies attribute data between two beans for all attributes with matching name. The copyProperties method was called both after the first, and the second page submit.

However what I found is that after the 2nd page submit the call overwrote the attributes saved from the first page with nulls.
Debugging showed that the ActionForm's reset() method is called at displaying either of the two form pages (as it should I guess).

My quick and dirty solution was not to use copyProperties after the 2nd form submit, but just set the user properties defined on the 2nd form. But I am not satisfied, as it is quite ugly.

Can you suggest any "nice" solution to this problem ?

Thanks in advance.
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are not using the reset() method of your form bean (i.e. its empty), then you could make "userForm" session scope and use the copy method after the second form submit only.

Sheldon Fernandes
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For future reference, reset() occurs upon every instance of entering into an ActionMapping.

I would recommend splitting your one ActionForm into two. They don't have any fields common to the two pages, correct?
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option is to clean out the reset method. Often it is not necessary to null out everything. Checkboxes are usually the only thing that require it.
 
Gyula Klinszky
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheldon, Marc, thanks for your help !

Leaving the reset() empty solves the problem, my only worry is that AFAIK ActionForm instances are often kept in memory instead of creating new ones for the new requests. If this is true, what happens if I call the page a 2nd time, and dont fill a particular field which had value in the 1st request ? Or - apart from checkboxes - the empty value overwrites the old one anyway ?
Meantime I came up with with another possible solution, if you have time, please give your opinion on this: What if I check the session for the User object in the reset() method, and do the cleaning up accordingly ? (Between the 2 pages of user creation the half-ready user object is kept in the session)
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leaving the reset() empty solves the problem, my only worry is that AFAIK ActionForm instances are often kept in memory instead of creating new ones for the new requests.


True. In your case, the forms are kept in session and Struts will reuse one if it is found in session scope.

If this is true, what happens if I call the page a 2nd time, and dont fill a particular field which had value in the 1st request ? Or - apart from checkboxes - the empty value overwrites the old one anyway ?


It will repopulate the jsp with the value from memory if you are using Struts tags for the inputs. Also, if the textfield is left empty, it will submit "", or empty string and overwrite the old one.


Meantime I came up with with another possible solution, if you have time, please give your opinion on this: What if I check the session for the User object in the reset() method, and do the cleaning up accordingly ? (Between the 2 pages of user creation the half-ready user object is kept in the session)


Isn't the half-ready user object in the ActionForm??? Why are you storing incomplete data in two places - both the form and as a separate session User object?
[ September 28, 2004: Message edited by: Marc Peabody ]
 
Gyula Klinszky
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad... I was storing the User object in the request, as an attribute Of course you are right, there is no point in duplicated storing.

A linked question: Is there a better method of storing the id of the record being modified on a page than storing it as a hidden variable on the page itself ?
[ October 06, 2004: Message edited by: Gyula Klinszky ]
 
He baked a muffin that stole my car! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic