So I’ve found a bunch of Struts 2 CRUD examples around the web:
and a few books:
Apache Struts 2 Web Application Development ISBN: 978-1847193391
Struts 2 Design and Programming ISBN: 978-0980331608
But all of them differ a little bit on how to do form population.
Some suggest implementing the Java interfaces ModelDriven or Prepareable to call come prepare function to pre-populate any needed data members.
Others suggest creating your own PrepareForUpdate action that calls a pre-populate function then redirects to the main edit view.
They also very on how to pass around an object identifier to indicate what object to retrieve for editing. SOme suggest intercepters what others throw it in the URL parameters and retrieve it through ActionContext or pass it around through a s:hidden field.
Is there a Best Practices way to do form population in Struts 2?
What are the advantages/disadvantages to the methods mentioned above?
I’m not aware of any documented best practices, but I’ve been using Webwork and Struts2 for about three years now, so I can tell you what I’ve used in my projects. By the way, the CRUD demo documentation you linked to strikes me as a bit out of date (I realize its from the project site).
I split my CRUD work into three different actions:
prepare()method to set up dropdowns, etc.That’s the approach that I would advocate, although I don’t use the
ModelDriveninterface. For details, check out how Struts2 ModelDriven interface works and the comments on my answer. Whether you useModelDrivenor not is just a personal choice. Also, check out why is model-driven action preferred over object backed bean properties.I have not seen that before and based on your description, I would avoid that technique. It seems wasteful to do a redirect and create an additional HTTP request to achieve the same thing that the
prepare()method was designed to handle.Just pass the identifier in the URL or the form. That’s the standard approach for web applications.