Struts

Class ActionForm in struts
adplus-dvertising
Previous Home Next

An ActionForm represents a JSP/ HTML form that the user interacts with over one or more pages. Also, an ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm which can be stored the form information in either the session (default) or request scope.The framework sets the ActionForm's properties from the request parameters and sends the validated form to the appropriate Action's execute method.

When you code your ActionForm beans class, keep the following principles in mind:

  • Typically, an ActionForm bean will have only property of setter and getter methods, with no business logic so don't required any specific methods to be implemented. Basically, it is used to identify the role which particular beans play in the overall architecture.
  • Define a property (with associated setXxx and getXxx methods) for each field that is present in the form. The field name and property name must match according to the usual JavaBeans conventions.
  • The ActionForm object also offers a standard validation mechanism. If you override a "stub" method, and provide error messages in the standard application resource, The framework will automatically validate the input from the form (using your method).
  • Remember, the ActionForm is meant to represent your data-entry form, not just the data beans. So, hyperlinks, buttons and other controls on form can also be defined as properties. This can help determine which button or control was selected when the form was submitted.
  • An ActionForm that fails validation will not even be presented to the Action for handling.
  • Beans may also activated by a bean instance in the form, also use nested property references. If you nest an existing bean instance on your form, think about the properties it exposes because any public property( setXxx and getXxx ) on an ActionForm that accepts a single String value can be set with a query string.
  • DynaAction Form class

    DynaActionForms relieve developers of maintaining simple ActionForms, because it is almost similar to the DynaBeans. In which just create the get/set methods for each of your bean's properties and list its properties in the framework's configuration( struts-config.xml) file.

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts-config PUBLIC"-
    //Apache Software Foundation
    //DTD Struts Configuration 1.3//EN"
    "http://jakarta.apache.org/struts/dtds
    /struts-config_1_3.dtd">
    <struts-config>
    <form-bean name="LoginForm" 
    type="org.apache.struts.action.DynaActionForm">
    <form-property name="name" type="java.lang.String"
    initial="consumer1" ></form-property>
    <form-property name="name" type="java.lang.String" 
    initial="consumer2" ></form-property>
    <form-property name="name" type="java.lang.String"
    initial="consumer3" ></form-property>
    <form-property name="name" type="java.lang.String"
    initial="consumer4" ></form-property>
     </form-bean>
    </struts-config>
    
    Previous Home Next