If you use:
<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>
and later on you use:
<bean:define id="someObject" name="baseForm" property="someObject"/>
you shouldn't have a problem. <jsp:useBean>
will create the object in the application scope if it does not exist and <bean:define>
should have no problem in retrieving it since it does a pageContext.findAttribute(...)
searching for all scopes.
If you get "Cannot find bean: "baseForm" in any scope" that means that the form ain't there when you call <bean:define>
(maybe you didn't call <jsp:useBean>
first?).
One other thing, application scope does not only mean that all JSP files will have access to the bean, but also all clients will share the same object. You can end up having problems if that bean has state that you share between users. One user could have access to confidential data of another.
EDIT: To understand this right... you just want to reference the current action form (whatever that will be) in a generic way in your JSP files, without depending on the name that you specified in struts-config (myCurrentActionForm, someOtherCurrentActionForm etc) and just using a qualifier like baseForm
?
If yes, than you could create yourself a parent Action
class that all your other action classes will extend. In the execute(mapping, form, request, response)
method of this parent class you just place a reference to the form
in the desired scope (you can extract the exact scope from the mapping
object or just store it in whatever scope you want, using the baseForm
name – I would stay away from the application scope).
You could then use the Template Method Pattern to call the subclass actions execute
methods, or you could resort to the subclass execute
method calling the parent execute
method to do its thing.
Once in the JSP you would then be able to access the form using baseForm
. The tags won’t care what the scope is because they will search until they find the object. If you stick to properties from the web.common.BaseForm
they won’t even care that it is a subclass instance.