• home
  • forum
  • my
  • kt
  • download
  • Struts DynaActionForm Tutorial

    Author: 2009-04-16 10:39:41 From:

    DynaActionForm Bean

    DynaActionForm Beans are the extension of Form Beans that allows you to specify the form properties inside the struts configuration file instead of creating a seperate concreate class. It will become tedious to create a seperate form bean for each action class. Using DynaActionForm we can easily create Form Bean in struts-config.xml file. The struts-config.xml file entry for the DyanActionForm Bean is shown below.

    1.<form-beans>
    2.    <form-bean name="LoginForm" type="org.apache.struts.action.DynaActionForm">
    3.        <form-property name="userName" type="java.lang.String" />
    4.        <form-property name="password" type="java.lang.String" />
    5.    </form-bean>
    6.</form-beans>

    The type attribute points to org.apache.struts.action.DynaActionForm and the <form-property> tag is used to define all the form variables. The <form-property> tag has the following three attributes.

    • name - The unique name of the property.
    • initial - The default value of the property.
    • type - Defines the Java type of the property. The available types are
      • java.math.BigDecimal
      • java.math.BigInteger
      • boolean and java.lang.Boolean
      • byte and java.lang.Byte
      • char and java.lang.Character
      • java.lang.Class
      • double and java.lang.Double
      • float and java.lang.Float
      • int and java.lang.Integer
      • long and java.lang.Long
      • short and java.lang.Short
      • java.lang.String
      • java.sql.Date
      • java.sql.Time
      • java.sql.Timestamp

    DynaActionForm Bean

    Now we will see how to access the DyanActionForm in the action class.

    01.public class LoginAction extends org.apache.struts.action.Action {
    02.  
    03.    public ActionForward execute(ActionMapping mapping, ActionForm form,
    04.            HttpServletRequest request, HttpServletResponse response)
    05.            throws Exception {
    06.        DynaActionForm loginForm = (DynaActionForm) form;
    07.        String userName = loginForm.get("userName").toString();
    08.        String password = loginForm.get("password").toString();
    09.        if(userName.equals(password) )
    10.        {
    11.            return mapping.findForward("success");
    12.        }
    13.        else
    14.        {
    15.            return mapping.findForward("failure");
    16.        }
    17.  
    18.    }
    19.}

    We need to typecast the form object to DynaActionForm object in the execute method of the action class. After that we can access the Form Bean properties. We will consider a simple login application for our example. Here we will check the user name and password, if they are equal then we will forward the user to the success page, else we will forward the user to the failue page.

    DynaActionForm Bean

    Now we will run the Login application. Lets enter the user name as "Eswar" and the password as "Eswar" and click the Login button.

    You can download the source code of the DynaActionForm example by clicking on the Download link below.

    Source :Download
    Source + Lib :Download

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Applet Building (6)
      Application Building (9)
      Communication (1)
      Database Related (12)
      Development (13)
      EJB (14)
      Game Programming (5)
      General Java (66)
      Javabeans (4)
      JSP and Servlets (15)
      Miscellaneous (28)
      Networking (1)
      Security (2)
      Swing (13)
      WAP and WML (2)
      XML and Java (4)

    New

    Hot