On Mon, 6 Aug 2001, Ronald Bakker wrote:

> Hi everybody,
> 
> In Struts, form-beans can be described by 'name' and 'type'. Looking at
> method 'processActionForm' in ActionServlet, I conclude that roughly the
> following is happening (correct me if I'm wrong):
> 
> When trying to find a form-bean from a mapping, Struts tries to find an
> attribute with the given 'name' in the scope that is defined for this
> mapping, if the class name of the found attribute is equal to 'type' it
> will reuse this found attribute. Otherwise a new form-bean instance of
> class 'type' is created and placed in scope. 
> 

That's a correct understanding.

> To my opinion, one fallback of this implementation is that you are
> forced to use the implementing class name of the form-bean you intend to
> use, you are not able to use an interface or abstract class name. Is
> there any chance Struts is going to use a more flexible way of
> retrieving form-beans from a mapping? I would like Struts to use the
> same mechanism as jsp:useBean, where you are more flexible in using
> beans with  either 'type' or 'class'.
> 

Remember that <jsp:useBean> has a restriction -- if the bean is not
already there in the correct scope, you *must* have specified the actual
class name so that <jsp:useBean> can instantiate it.  Struts will often
(and, if you're using request scope form beans, *always*) need this
information anyway, for the same reason.

> Right now, we replaced this part of processActionForm with an own
> mechanism of reusing form-beans:
> 
> /**
>  * Original Struts code:
>  */
> 
> // Can we recycle the existing form bean instance?
> if ((instance != null) &&
>   className.equals(instance.getClass().getName())) {
>   if (debug >= 1) {
>     log(" Recycling existing ActionForm bean instance of class '"
>         + className + "'");
>   }
> 
>   return (instance);
> }
> 
> /**
>  * Replaced by our code:
>  */
> 
> // Can we recycle the existing form bean instance?
> try {
> 
>   if ((instance != null) &&
> Class.forName(className).isInstance(instance)) {
>     if (debug >= 1) {
>       log(" Recycling existing ActionForm bean instance of class '"
>           + className + "'");
>     }
>     return (instance);
>   }
> }
> catch (ClassNotFoundException ce) {
> 
>   if (debug >= 1) {
>     log(" Class not found: '" + className + "'");
>   }
>   return null;
> }
> 

I can see what you're after, but won't this only work in the case of
session beans that were previously created by an Action?  For anyone else,
you'll just get an instantiation error.

Note to self -- if we change the logic here, a similar change will be
needed inside the <html:form> tag, which can also create the form bean
dynamically if needed.

> Ronald Bakker
> Virgil PM&C
> 

Craig


Reply via email to