tags:

views:

150

answers:

2

Hi All, I'm using struts 1.3.10

In JSP I can reference an object from the form that's associated with the action. So I can have:

<bean:define id="someObject" name="myCurrentActionForm" property="someObject"/>

Where I have my form defined in struts-config.xml.

In my case above "someObject" is actually defined in the base class that myCurrentActionForm inherits from.

But I need to reference the "someObject" in a generic way.

I tried:

<bean:define id="someObject" name="baseForm" property="someObject"/>

using the reference to the base form class but I get an error that the property could not be found.

I need to define this bean in a common JSP snippet. Is there a way to reference the form associated with the action in a generic way?

TIA

A: 

Here's how I did it.

<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>

This creates a single instance at the application scope that I can use.

Dan Howard
A: 

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.

dpb
Right. That's the other problem. Another one I have is that the base form is a new instance. I want to have the real action form instance but referenced in a generalized way. :(
Dan Howard
See my edited answer.
dpb