You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "Brinkhuis, Erwin" <Er...@iWise.nl> on 2000/12/06 15:42:08 UTC

Enforcing form-bean type

(Referring to the latest nightly build (jakarta-struts-20001206))

When using the attribute "attribute" of an "action" element in the
struts-config.xml allows specifying the same attribute for different
form-beans with different types.

This is very handy as it allows polymorphic usage of actionforms. By doing
so one can also keeping the size of the session object to a minimum (one
actionform instance at a time, not multiple). Our business application makes
use of this feature of struts. But only after we did override the
processActionForm method of the ActionServlet.

This method initially checks for an existing instance (only using the
attribute name). If an instance is found (regardless of its type), it is
returned. If however the specified type for the actionform is not equal the
class of the found instance, the found instance is still returned leading to
possible class cast exceptions. We did an override of the method and
included a check for class - type match, and if they do not match a new
actionform instance of the requested type is created replacing the incorrect
instance.

In my opinion, it would be an enhancement of struts to be able to use one
attribute mapping to different actionform types. By implementing this
suggested type check, the current struts functionality is not endangered as
it only prevents a runtime exception and supports more flexibility when
using the attribute "atribute".


Erwin Brinkhuis.


Below is our override of the processActionForm method of the ActionServlet
including the type check.

  protected ActionForm processActionForm( ActionMapping      mapping
                                        , HttpServletRequest request )
  {
    // Is there a form bean associated with this mapping?
	  String attribute = mapping.getAttribute();
    if (attribute == null)
      return (null);

    // Look up the existing form bean, if any
    if (debug >= 1)
      log(" Looking for ActionForm bean under attribute '" + attribute +
"'");

    ActionForm instance = null;
    HttpSession session = null;
    if ("request".equals(mapping.getScope()))
    {
      instance = (ActionForm) request.getAttribute(attribute);
    }
    else
    {
      session = request.getSession();
      instance = (ActionForm) session.getAttribute(attribute);
    }

    String name = mapping.getName();
    String className = null;
    ActionFormBean formBean = findFormBean(name);
    if (formBean != null)
      className = formBean.getType();
    if (className != null)
    {
      if ( instance == null ||
!className.equals(instance.getClass().getName()) )
      {
        // Create a new form bean if we need to
        if (debug >= 1)
         log(" Creating new ActionForm instance");
        try
        {
          Class clazz = Class.forName(className);
          instance = (ActionForm) clazz.newInstance();
        }
        catch (Throwable t)
        {
          log("Error creating ActionForm instance of class '" + className +
"'", t);
        }
      }
      else
      {
        // Form bean class matches instance class
        if (debug >= 1)
         log(" Reusing existing ActionForm instance");
      }
    }
    else
    {
      instance = null;
    }
    
    if (instance == null)
      return (null);

    // Store the newly created bean in the appropriate scope
    if (debug >= 1)
      log(" Storing instance under attribute '" + attribute + "'");

    if ("request".equals(mapping.getScope()))
      request.setAttribute(attribute, instance);
    else
      session.setAttribute(attribute, instance);
    return (instance);
  }

-----------------------------------------------------------
 Erwin Brinkhuis                    iWise B.V.             
                                    Hoofdstraat 2a-4a      
 mailto:erwin.brinkhuis@iwise.nl    4941 DC Raamsdonksveer 
 Phone  ++31 (0)162 517167          The Netherlands        
 Fax    ++31 (0)162 516872          http://www.iwise.nl    
-----------------------------------------------------------