One of the questions I see asked over and over on the Struts Users mailing list, in various related forms, is along the lines of "I have a dropdown that appears on three different pages of my site, and in all three it has the same content. How can I easily 'prepopulate' the page with the dropdown contents for all three pages?" There are of course any number of answers to that question, all with their own benefits and problems, some better than others, some worse. What there is not, currently in the 1.2.x branch at least, is a "standard" way of doing this.
setupItems was originally a proposal I made for such a standard approach. For various valid reasons however, it was not met with enthusiasm by the Struts team and so was ultimately not seriously considered for addition to the framework. However, I have now found that a number of people are using the code I posted in Bugzilla (http://issues.apache.org/bugzilla/buglist.cgi?cmdtype=runnamed&namedcmd=setupItems%20%28Struts%29) and many more have expressed some degree of interest in the idea. So, while it won't be added to Struts itself, evidence would suggest it is a worthwild addition to the Wiki. So, here it is!
In short, setupItems will allow you to do the following in struts-config.xml:
<action path="/test5" type="com.omnytex.setupexample.action.TestAction" parameter="method"> <setupItem setupClass="com.omnytex.setupexample.setups.SetupClass1" setupMethod="setupMethod1" /> <forward name="defaultForward" path="/result.jsp"> <setupItem setupClass="com.omnytex.setupexample.setups.SetupClass2" setupMethod="setupMethod1" /> </forward> </action>
What will happen when test5 is requested is that SetupClass1.setupMethod1() will be executed BEFORE execute() of TestAction is called. Then, if defaultForward is returned from execute(), SetupClass2.setupMethod1() will be called. Both are passed a reference to the current request object. The "setup classes" are POJOs, so there is no restriction on what you can or can't do. There are of course practical limitations, for instance, you probably don't want to be doing lengthy database work in them, especially if they will be executed or not, but that is still entirely up to you.
The setupItem elements can be added to Action mappings, local forwards or global forwards. You can add as many as you wish to each element, and they will be executed in the order they appear.
How To Use It In Your Own Application
In short, download the archive attached to the above referenced Bugzilla ticket. Or better yet, just get it here: setupexample.zip. Included in it is an updated struts.jar file (based on Struts 1.2.6). Replace your existing struts.jar file. Then, update your struts-config.xml replacing the doctype with:
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "WEB-INF/struts-config_1_2.dtd">
You should at that point be all set to use setupItems. Also included in that archive is a complete sample webapp that you should be able to just drop in your servlet container of choice and fire up. It shows every possible combination of setupItem usage (if I didn't miss any!) and should give you a good demonstration of how it works.
If you so choose, or need to, the full source is of course included, as well as an Ant build script that will recompile everything and update the struts.jar.
The following Struts classes are altered for this:
And a new class SetupItemConfig is added. Also, an updated DTD for struts-config.xml is needed and included in the archive and struts.jar file. If you wish to see specifically what was changed or added to any of those classes, simply search for the string "zammetti" in them as I surrounded any section of changed or added code with a comment including my name.
It is my sincere hope that people find this useful. It should be noted that this has already been turned down for addition to the 1.3 branch, as there is already a good solution for doing this built in to that, namely Commands, and there are no new features being added to the 1.2 branch. So, if you decide to use this, be forewarned: YOUR APPLICATION WILL ALMOST CERTAINLY NOT BE COMPATIBLE WITH STRUTS 1.3! Since there is no pending release of 1.3 at the moment, and since people are already finding this useful, I wanted to make it available to a wider audience than before. But, you absolutely should be using this with your eyes open and with full knowledge of the almost certain forward-incompatibility.