You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Ronald Bakker <ro...@virgil.nl> on 2001/08/06 11:58:05 UTC

Type of form-beans

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. 

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'.

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;
}

Ronald Bakker
Virgil PM&C