Asleson, Ryan wrote:
Hello, We're transitioning from a Struts 1.x and Tiles development environment
to Struts 2 and Sitemesh, and I need some help getting around some of
the differences.
Here's what I'm planning so far. I want to have an abstract BaseAction
that extends ActionSupport.  Since we are no longer using Tiles, we
can't count on using Tiles Controller classes to prepare a page for
display -- for example, retrieving the contents of drop down lists from
the database.  In my BaseAction, I have overridden the execute() method
to call a "lifecycle" method called prepareAction() that is meant to be
an extension point for subclasses to retrieve any data the is needed to
display the page.

Why add a custom lifecycle method? Why not just let sub-classes implement Preparable?

For example, let's say there's a drop-down select box on a page, and its
contents come from a database.  A concrete implementation of BaseAction
should override prepareAction() to retrieve the list items for the drop
down list.

By implementing Preparable that logic would go in either prepare() or prepareInput() (where 'input' is the name of the method on the action that is called to display the form).

Now, here's where things get sticky:  Assume the page above is
displayed.  The user enters some form data and posts the page.  However,
there are some validation errors.  Struts 2 will automatically redisplay
the page.... but will the drop-down list be populated with list items,
or will it be empty?

If there are validation errors, Struts will forward control to the action result mapped as 'input'. You can either arrange for the drop-down list data to be loaded before validation fires (e.g. using Preparable.prepare()) or, if you don't want to perform that load during a successful form submit, you can do it as part of the 'input' result processing.

In the Struts 1.x/Tiles world, where we have a Tiles Controller backing
the JSP, the list items would be repopulated by the Controller.  But
with Tiles no longer in play, what will repopulate the list?  I'm pretty
sure that the prepareAction() method that was used when the page is
first displayed will not be called when there are validation errors,
resulting in an empty drop-down list.

If a validation error occurs, Struts will not call the action method for handling the form submit so no, you can't rely on a method being called from there in this case.

So, here are my questions:
1. Is my idea to have a BaseAction that provides an extension point for
subclasses, where things like database access should occur, a bad idea?

Not necessarily a bad idea, but not necessarily optimal. For example, since your actions would all be dependent on super.execute() being invoked, it may make it messy to target other methods on the action (e.g. calling action.input() to setup and display a form and action.execute() to process it on submit).

2.  What is the recommended way to repopulate drop-down selects after a
validation error, if the list items need to be retrieved from a
database?

Take a look at the Preparable interface and the 'prepare' interceptor for one approach. Another may be to use the 'action-chain' result type for your action's 'input' result mapping, and keep the data retrieval in the chained action.

I recommend taking a look at some of the example applications (e.g. Mailo Reader and Showcase) to see how this has been handled elsewhere. There's More Than One Way To Do It (TM) :-)

L.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to