Details
Description
This was prompted by a recent question on the User's list, although I have
seen similar questions come up many times...
At the end of a typical Action, a developer does:
return mapping.findForward("someForward");
This will, typically, forward to some JSP. It is sometimes desireable to pass
parameters to that JSP, or to the next Action if the developer chooses to
forward to a mapping. It is also sometimes desireable to be able to send an
anchor along so that the browser will jump to the named anchor on the returned
page.
Both of these can be accomplished by "cloning" the returned forward and
manually setting the path appropriately. However, I believe this is more work
than it really needs to be. Even though it's not a big deal to do, we've
probably all done it at some point, it does require a developer to know the
internals of Struts a little more than they maybe should (i.e., they have to
understand that a ForwardConfig is frozen and has to be cloned to alter its
fields), and I believe this modification removes that neccessity.
The attached patch enables this capability in a more developer-friendly way,
namely that they can simply append the query string and/or anchor to the
forward name requested, like so:
return mapping.findForward("someMapping?myParam=myVal#myAnchor");
In most cases this will probably be constructed dynamically in the Action, but
the result is the same.
The patch affects four classes: RequestProcessor, PerformForward,
ActionMapping and ActionForward. ActionForward simply has two new fields and
associated bean methods, "queryString" and "anchor". RequestProcessor and
PerformForward have identical new code, namely two lines that simply append
the query string and anchor to the URI forwarded or redirected to.
ActionMapping is where the more involved (relatively-speaking) changes are,
but then only in the findForward() method (as well as one new import).
Attached is also a dirt-simple sample app to demonstrate this working. Should
just be able to drop it in any container and give it a wirl.
It is my hope that enough people find this useful (and find no problems with
it of course!) to warrant being committed for the 1.3 release. I think more
than a few people will thank us for it